From 30a2f6df7b6488a773561649503c018880ff99da Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Mon, 9 Dec 2024 12:17:53 -0600 Subject: [PATCH] 2024 day 8 Complete! --- 2024/day08/input | 50 ++++++++++++++++++++++++++++++++++++ 2024/day08/main.go | 59 +++++++++++++++++++++++++++++++++++++++++++ 2024/day08/testinput | 12 +++++++++ 2024/day08/testinput2 | 10 ++++++++ helpers/coordinate.go | 7 +++++ 5 files changed, 138 insertions(+) create mode 100644 2024/day08/input create mode 100644 2024/day08/main.go create mode 100644 2024/day08/testinput create mode 100644 2024/day08/testinput2 diff --git a/2024/day08/input b/2024/day08/input new file mode 100644 index 0000000..a0e2233 --- /dev/null +++ b/2024/day08/input @@ -0,0 +1,50 @@ +............s...............1..................... +......................E......3.....S.............. +.......................3.....S.................... +...e........T.t.......S.1...........I............. +..................B..................I.....O...... +g.......z........i39......B..I.................... +.......s....S.......3......................i..I... +....e.............2..........B.................... +.......tC...z.......g......1...................... +.E......s....R.................................... +..G...t..........2................................ +.........K...C.......2............................ +....T..e...........5...C.......................... +...T................................O...o......... +...............................g..............o... +.........z...................g......i............o +...9.E............H...........Y.......O........... +..........R..H...............7.O.................. +...........H.............v......7........B........ +..9.Q.......................W......1........Y..... +.........................z.7.................Y.... +.....Q................................v........... +....K.......E.....R...............2..........o.... +.n............H......v...........................Y +.G.y..........................Q................... +......G....A5.....................h............... +..........D...5.w...9............................. +......n....5...L.................................. +............................v..................... +............L...0t..........7..................... +..n....k............y....................W........ +..k..........0.........................W.......... +...n.......R..L..a........................W....... +.........................................h........ +..0..L........c...b............................... +.....................8.y.......................... +.......w.................6.............h.......N.. +..........y..4.................................... +...0....8...k.....Z........r...................... +..............a...8Z.........G......4............. +........4..b.q.....................K.............. +.q...........kZ.K......b..D.........d............. +.8.....................D................r......... +.....w.........a...............d........A......... +................................d.A.hV............ +................c..........D.....V....r........... +.......Z......6.....l........................A.d.. +...................l..6..c....b......r...........N +......a....4........q..l..V..c................N... +l.....w...........q..6............V............... diff --git a/2024/day08/main.go b/2024/day08/main.go new file mode 100644 index 0000000..b47090c --- /dev/null +++ b/2024/day08/main.go @@ -0,0 +1,59 @@ +package main + +import ( + "fmt" + + h "git.bullercodeworks.com/brian/adventofcode/helpers" +) + +func main() { + inp := h.StdinToStringSlice() + part1(inp) + fmt.Println() + part2(inp) +} + +func part1(inp []string) { + c := h.StringSliceToCoordByteMap(inp) + anC := h.StringSliceToCoordByteMap(inp) + nodes := c.FindAllNot('.') + for k, bt := range nodes { + fNodes := c.FindAll(bt) + for _, nd := range fNodes { + if k.Equals(nd) { + continue + } + diff := k.Sub(nd) + an := k.Add(diff) + if anC.ContainsCoord(an) { + anC.Put(k.Add(diff), '#') + } + } + } + fmt.Println("# Part 1") + fmt.Println("Antinodes:", anC.Count('#')) +} + +func part2(inp []string) { + c := h.StringSliceToCoordByteMap(inp) + anC := h.StringSliceToCoordByteMap(inp) + nodes := c.FindAllNot('.') + for k, bt := range nodes { + fNodes := c.FindAll(bt) + for _, nd := range fNodes { + anC.Put(nd, '#') + anC.Put(k, '#') + if k.Equals(nd) { + continue + } + diff := k.Sub(nd) + an := k.Add(diff) + for anC.ContainsCoord(an) { + anC.Put(an, '#') + an = an.Add(diff) + } + } + } + fmt.Println("# Part 2") + fmt.Println("Antinodes:", anC.Count('#')) +} diff --git a/2024/day08/testinput b/2024/day08/testinput new file mode 100644 index 0000000..78a1e91 --- /dev/null +++ b/2024/day08/testinput @@ -0,0 +1,12 @@ +............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............ diff --git a/2024/day08/testinput2 b/2024/day08/testinput2 new file mode 100644 index 0000000..b8439c3 --- /dev/null +++ b/2024/day08/testinput2 @@ -0,0 +1,10 @@ +T......... +...T...... +.T........ +.......... +.......... +.......... +.......... +.......... +.......... +.......... diff --git a/helpers/coordinate.go b/helpers/coordinate.go index a98fbd5..36adac1 100644 --- a/helpers/coordinate.go +++ b/helpers/coordinate.go @@ -42,6 +42,13 @@ func (c *Coordinate) MoveNW() { c.Y-- } +func (c Coordinate) Sub(o Coordinate) Coordinate { + return Coordinate{ + X: c.X - o.X, + Y: c.Y - o.Y, + } +} + func (c Coordinate) Add(o Coordinate) Coordinate { return Coordinate{ X: c.X + o.X,