From ae293fe5a25af2cf1cebbdcd7484c7b3493b1cfb Mon Sep 17 00:00:00 2001 From: Gareth Date: Fri, 6 Dec 2024 16:56:58 +0000 Subject: [PATCH] Day06 --- 2024/gareth/day06/day06.go | 142 ++++++++++++++++++++++++++++++++ 2024/gareth/day06/day06_test.go | 35 ++++++++ 2024/gareth/day06/input.txt | 130 +++++++++++++++++++++++++++++ 2024/gareth/main.go | 12 ++- 4 files changed, 315 insertions(+), 4 deletions(-) create mode 100644 2024/gareth/day06/day06.go create mode 100644 2024/gareth/day06/day06_test.go create mode 100644 2024/gareth/day06/input.txt diff --git a/2024/gareth/day06/day06.go b/2024/gareth/day06/day06.go new file mode 100644 index 0000000..08e95bc --- /dev/null +++ b/2024/gareth/day06/day06.go @@ -0,0 +1,142 @@ +package day06 + +import ( + "strings" +) + +type Point struct { + x int + y int + direction int +} + +const ( + Up = 0 + Right = 1 + Down = 2 + Left = 3 +) + +var rows, cols int + +var moves = []Point{ + {-1, 0, Up}, + {0, 1, Up}, + {1, 0, Up}, + {0, -1, Up}, +} + +func Part1(input string) int { + grid, start, direction := ParseInput(input) + total := PredictPath(grid, start, direction) + return total +} + +func Part2(input string) int { + grid, start, direction := ParseInput(input) + total := FindLoopingPositions(grid, start, direction) + return total +} + +func ParseInput(input string) ([][]rune, Point, int) { + lines := strings.Split(strings.TrimSpace(input), "\n") + rows = len(lines) + cols = len(lines[0]) + grid := make([][]rune, rows) + + var start Point + var direction int + + for i := 0; i < rows; i++ { + grid[i] = []rune(lines[i]) + for j, char := range lines[i] { + if char == '^' { + start = Point{i, j, Up} + direction = Up + } else if char == '>' { + start = Point{i, j, Right} + direction = Right + } else if char == 'v' { + start = Point{i, j, Down} + direction = Down + } else if char == '<' { + start = Point{i, j, Left} + direction = Left + } + } + } + + return grid, start, direction +} + +func PredictPath(grid [][]rune, start Point, direction int) int { + visited := make(map[Point]bool) + current := start + + visited[current] = true + + // do while + for { + next := Point{current.x + moves[direction].x, current.y + moves[direction].y, Up} + + if next.x < 0 || next.x >= rows || next.y < 0 || next.y >= cols { + break + } + + if grid[next.x][next.y] == '#' { + direction = (direction + 1) % 4 + } else { + current = next + visited[current] = true + } + } + + return len(visited) +} + +func FindLoopingPositions(grid [][]rune, start Point, direction int) int { + possiblePositions := 0 + + for i := 0; i < rows; i++ { + for j := 0; j < cols; j++ { + if grid[i][j] != '.' || (i == start.x && j == start.y) { + continue + } + + grid[i][j] = '#' + if IsLooping(grid, start, direction) { + possiblePositions++ + } + grid[i][j] = '.' + } + } + + return possiblePositions +} + +func IsLooping(grid [][]rune, start Point, direction int) bool { + visited := make(map[Point]int) + current := start + + step := 0 + for { + step++ + next := Point{current.x + moves[direction].x, current.y + moves[direction].y, direction} + + if next.x < 0 || next.x >= rows || next.y < 0 || next.y >= cols { + return false + } + + if grid[next.x][next.y] == '#' { + direction = (direction + 1) % 4 + } else { + current = next + + if visitStep, ok := visited[current]; ok && step-visitStep > 4 { + return true + } + + visited[current] = step + } + } +} diff --git a/2024/gareth/day06/day06_test.go b/2024/gareth/day06/day06_test.go new file mode 100644 index 0000000..f4a30da --- /dev/null +++ b/2024/gareth/day06/day06_test.go @@ -0,0 +1,35 @@ +package day06 + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestPart1(t *testing.T) { + r := Part1(`....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#...`) + assert.Equal(t, 41, r) +} + +func TestPart2(t *testing.T) { + r := Part2(`....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#...`) + assert.Equal(t, 6, r) +} diff --git a/2024/gareth/day06/input.txt b/2024/gareth/day06/input.txt new file mode 100644 index 0000000..b2491bc --- /dev/null +++ b/2024/gareth/day06/input.txt @@ -0,0 +1,130 @@ +#.............#.#...........................................................#..........#...........................#..........#... +............#......................#.......#..#.........#...................##.................................................... +..............#...........#..#.......#.........#.#...........................#................................#......#............ +.....#......................................#......#..#...................#....................#......................#........... +...#............................#................................#................................................................ +..........#..................#.....#......................................................#....................................... +..#........#.......#.#...............................................................#......#.................................#... +.#....#................#................#.....#...................#...............................................#..#............ +.......................................................................................#...#.#....#........##......#........#.#.## +............................................#.................#.....................#..........................#.................. +....#..........##......................#.................................................................................#........ +......#.............##.........#.......#.........#.......#.............#...#.......#.............................................. +.#............................................................................#..........#....#................................... +...#..#....#..............................#...........................................................................#........... +.............#.................................................................................................................... +................................#..................##...................................#...............................#......#.. +..........#.........#.................#..........###...........................................#............#...#................. +.............#....................................#.......#.#.#............#............................................#......... +......#.#..............................................................................................#.........................# +...#...#.........#.........#..#......#................##.................#........................................................ +.................................#......................#........#................#........................#...................... +.......................##..............#................#................#..............................#......................... +.......#...#.....#.....##........................#..................##.....#...................................................... +.............................................#...........................#..........................................#.....#....... +.........................#...............#....#..............#.......#...............#.#.................#.................#...... +...........................................................#....#..................................#.............................. +..........................#.................................................#............................##....#.................. +.................#...#.........................................................#........................#.....#..#................ +.........................#....#................................................#...#.....................#....#...........#....... +................................................##.......#.#....................................#.......##........................ +..........##...#.......................#....#..#....................#........................#.................................... +#......#.......................#............#.....................................................................#............... +.......#.......#...#..#..........#.......................................#.............#......#................................... +...............#............#..................#.................................................................................# +.......#...........#.......................##.....#.........#...........#...................#......#.........#.............##....# +...................#.........................#......#..........................................#..........#.........#......#...... +.....#............#..............#...#......#....................#.............#.................................................. +................................................#........#.....#...................#.....................................#.....#.. +......................#.#...##......................##...............................................#.................#.......... +#...................#.........................#...........#.....#................#.....................#...................#...... +....#................#.....#.............#.....................................#....#...#.................................#....... +.........#.....................................................................................................#.#.......#........ +.........#..........##..............................#............................#.#.............................#................ +#....#.#..............................................##...#...#...............#.............#.................................... +........#..#.#...................#.......................................................................#.............#..#.#...#. +.................................................................................................................#.......#........ +............................................#..#...............##........#..........#.......................................#....# +........#...........................#............................#.....#........................#..........#...#..........#....... +.#...........#...................................................#...........................................#................#.#. +............#...............................#........#...........#..........#...........#.#..............#........................ +...............................................................................##.#................#.............................. +.#.......................#.#...........#..................................#....................................................... +..#..............................................##................#.......#....#..............#.....................#...#........ +...................#.....#................................................#..........#.........#.........#.....................#.. +........#................#................................................................................#......#................ +...................#...#....#.........................#.........................#....................#..........................#. +.................#.......................................................#................................................#....... +..#...........................##.#.............#..#............................................#.......................#.#........ +..#......#.........................................................................#.......................#...................... +...........................................................................................................#............#......#.. +............................................................^..........................................#..........#............... +..#.........#....................#..........................................#........#.........................................#.. +........#...............#....#.............#......#.........................................................................#..... +.........#......#.....#..............#...........................................................#..............#................. +.........#...................................................#...###...........#.......#.##....................................... +.....................##.................#.........##.....................#................#................#.........#............ +.......................................................................#..............#..............#.................#.......... +.##.........#..........................................................................#....................................#..... +.........................#.............................#........................................................................#. +........#.............................................#......#....#.....................................#....................#.... +........#...#..............................................................................#.........#............................ +..............................#................................#..........#..............#..................................#..... +........##..#...................................#................................#.................#.........#......#..#........#. +..................#......#...................................................................#.................................... +..........#.............#..............#........#............#...............#..#..........#..............#.#...#............#.... +............................##....#..............#......................................................................#......... +...............................................#.....#...........#.....##...............#..#...................................... +.........................#.......#...............................#...........#.........#...........#....#......................... +.#............#...#...............#..............................................................#..........................#....# +#...........#......................................................#.............................#......#......................... +..................................................#....#...............#.........................#................................ +................................................#..........................#.......#..............................#....##.......#. +........#......#..#............#................#............................#......#.##.........#....................#..#........ +.......................................................#..#.#....................#..#......................................#...... +.....................#.......................................................................#............#....................... +.....................##........................#...................#.....................#........................................ +..........................#...........................................................................................#........... +..#..........#...................#..............##..#.....................#...................#..............#...........#........ +#................................#....##......#.............#..............#.......................#.........#...........#........ +..................#................#...............................#.....#..........#................#..............#............. +................#.#.........#.....##...............................................................#..............#............... +..#...............................................................................................#............................... +..........#.................#...........#.......#.........................#...#.......#........................................... +..##....#......................................#.#.......................................#.................#...................... +.......................#..#...#.##......................................................................#.........#...#........... +...............#.........#..............#.....................................................#...................#............... +......#.................#............#......#...................#..#.........................#........................#........#.. +......................................................................#...........................#........#...................... +..........#..........#........................#.............##.................................................................... +....#................................................#.......................#................#...............#................... +.................#....................................................#......#.................................................... +.#........#....................................................................................................................... +......##..................#....#......#.................#....................#.....................#..#...........#............... +#...#...#...#..............#........#.......................#.#..........#...............................#......#................. +#...#...................#.............#........#..........#...............................................................#...#... +..#................................#...#.........................#...................#......#..................................... +...#...###..........#............#...............................................................#..........#..................... +............#..............#.......#.....#..........#.....#.........................................................#............# +..#......................#..#...#................................#......#.......#.....#..................#.........#.............. +....##......#.......................................................#..............................................#.............. +...............................#.....#....#.................................................#.......#............#................ +..........#....#......................#........#..........................#......#................................................ +......#.............................#...........#..................#...........................#................................#. +...........................##..........................................#.....#.#.....................#................#........... +..#..............#.........................................................#........................#.........#.................#. +#.....#.....#......................#....................#........................................#...........#...........#......#. +.......................................#..#................................#.........#.......#..#.....#..........................# +..........#..............##...............#..........................................................#......#......#......#....#.. +.............#..............#..#......#........#..#......##.......................#...........#.......#........................... +......................#.......................................#.......#..#.....................................#.........#...##... +...........................#...........................#......#....#.............................#.............................#.. +..................#............................#....#........##.........................#..............#.............#............ +.......#....................#.....#........#.......#.#...#...............#.....#.#.........................#.......#.#............ +.....#.........#.......#..........#.................#..#...#..................................................##.................. +................#........................................................#........................................................ +....##......#........#..................#.........................#...................................#...#...................#... +..............#....................#.......#.......................#.............#......#..............#.......................... +...........................#..............#.....#............#.....................#........#....#.......................#........ +........#..##................#.....#.#..............#..............................#........................#.........#........... +.........................................................#...........................##..........#........#.##.................... \ No newline at end of file diff --git a/2024/gareth/main.go b/2024/gareth/main.go index fc2a0f4..7c66f1b 100644 --- a/2024/gareth/main.go +++ b/2024/gareth/main.go @@ -1,13 +1,17 @@ package main import ( - "aoc2024/day05" + "aoc2024/day06" "fmt" "os" + "time" ) func main() { - data, _ := os.ReadFile("day05/input.txt") - fmt.Printf("part 1: %d\n", day05.Part1(string(data))) - fmt.Printf("part 2: %d\n", day05.Part2(string(data))) + start := time.Now() + data, _ := os.ReadFile("day06/input.txt") + fmt.Printf("part 1: %d\n", day06.Part1(string(data))) + fmt.Printf("part 2: %d\n", day06.Part2(string(data))) + elapsed := time.Since(start) + fmt.Printf("Execution time: %s\n", elapsed) }