diff --git a/2023/21/README.md b/2023/21/README.md new file mode 100755 index 0000000..7c8a7dd --- /dev/null +++ b/2023/21/README.md @@ -0,0 +1,100 @@ +## \-\-\- Day 21: Step Counter --- + +You manage to catch the [airship](7) right as it's dropping someone else off on their all-expenses-paid trip to Desert Island! It even helpfully drops you off near the [gardener](5) and his massive farm. + +"You got the sand flowing again! Great work! Now we just need to wait until we have enough sand to filter the water for Snow Island and we'll have snow again in no time." + +While you wait, one of the Elves that works with the gardener heard how good you are at solving problems and would like your help. He needs to get his [steps](https://en.wikipedia.org/wiki/Pedometer) in for the day, and so he'd like to know _which garden plots he can reach with exactly his remaining `64` steps_. + +He gives you an up-to-date map (your puzzle input) of his starting position ( `S`), garden plots ( `.`), and rocks ( `#`). For example: + +``` +........... +.....###.#. +.###.##..#. +..#.#...#.. +....#.#.... +.##..S####. +.##..#...#. +.......##.. +.##.#.####. +.##..##.##. +........... + +``` + +The Elf starts at the starting position ( `S`) which also counts as a garden plot. Then, he can take one step north, south, east, or west, but only onto tiles that are garden plots. This would allow him to reach any of the tiles marked `O`: + +``` +........... +.....###.#. +.###.##..#. +..#.#...#.. +....#O#.... +.##.OS####. +.##..#...#. +.......##.. +.##.#.####. +.##..##.##. +........... + +``` + +Then, he takes a second step. Since at this point he could be at _either_ tile marked `O`, his second step would allow him to reach any garden plot that is one step north, south, east, or west of _any_ tile that he could have reached after the first step: + +``` +........... +.....###.#. +.###.##..#. +..#.#O..#.. +....#.#.... +.##O.O####. +.##.O#...#. +.......##.. +.##.#.####. +.##..##.##. +........... + +``` + +After two steps, he could be at any of the tiles marked `O` above, including the starting position (either by going north-then-south or by going west-then-east). + +A single third step leads to even more possibilities: + +``` +........... +.....###.#. +.###.##..#. +..#.#.O.#.. +...O#O#.... +.##.OS####. +.##O.#...#. +....O..##.. +.##.#.####. +.##..##.##. +........... + +``` + +He will continue like this until his steps for the day have been exhausted. After a total of `6` steps, he could reach any of the garden plots marked `O`: + +``` +........... +.....###.#. +.###.##.O#. +.O#O#O.O#.. +O.O.#.#.O.. +.##O.O####. +.##.O#O..#. +.O.O.O.##.. +.##.#.####. +.##O.##.##. +........... + +``` + +In this example, if the Elf's goal was to get exactly `6` more steps today, he could use them to reach any of `16` garden plots. + +However, the Elf _actually needs to get `64` steps today_, and the map he's handed you is much larger than the example map. + +Starting from the garden plot marked `S` on your map, _how many garden plots could the Elf reach in exactly `64` steps?_ \ No newline at end of file diff --git a/2023/21/code.go b/2023/21/code.go new file mode 100644 index 0000000..1c4d363 --- /dev/null +++ b/2023/21/code.go @@ -0,0 +1,89 @@ +package main + +import ( + "fmt" + "strings" + + "github.com/jpillora/puzzler/harness/aoc" +) + +func main() { + aoc.Harness(run) +} + +// on code change, run will be executed 4 times: +// 1. with: false (part1), and example input +// 2. with: true (part2), and example input +// 3. with: false (part1), and user input +// 4. with: true (part2), and user input +// the return value of each run is printed to stdout +func run(part2 bool, input string) any { + grid := make([][]rune, 0) + v, h := 0, 0 + for y, l := range strings.Split(input, "\n") { + if l == "" { + continue + } + + gridLine := make([]rune, 0) + for x, s := range strings.Split(l, "") { + if s == "S" { + v, h = y, x + } + gridLine = append(gridLine, rune(s[0])) + } + grid = append(grid, gridLine) + } + + steps := 64 + + // when you're ready to do part 2, remove this "not implemented" block + if part2 { + return "not implemented" + } + // solve part 1 here + + possibleCells := [][]int{{v, h}} + + for i := 0; i < steps; i++ { + nextStepCells := make([][]int, 0) + alreadyStepped := make(map[string]bool) + + for _, pc := range possibleCells { + + if pc[0] > 0 && grid[pc[0]-1][pc[1]] != '#' { + if _, ok := alreadyStepped[fmt.Sprintf("%v;%v", pc[0]-1, pc[1])]; !ok { + nextStepCells = append(nextStepCells, []int{pc[0] - 1, pc[1]}) + alreadyStepped[fmt.Sprintf("%v;%v", pc[0]-1, pc[1])] = true + } + } + + if pc[1] > 0 && grid[pc[0]][pc[1]-1] != '#' { + if _, ok := alreadyStepped[fmt.Sprintf("%v;%v", pc[0], pc[1]-1)]; !ok { + nextStepCells = append(nextStepCells, []int{pc[0], pc[1] - 1}) + alreadyStepped[fmt.Sprintf("%v;%v", pc[0], pc[1]-1)] = true + } + } + + if pc[0] < len(grid)-1 && grid[pc[0]+1][pc[1]] != '#' { + if _, ok := alreadyStepped[fmt.Sprintf("%v;%v", pc[0]+1, pc[1])]; !ok { + nextStepCells = append(nextStepCells, []int{pc[0] + 1, pc[1]}) + alreadyStepped[fmt.Sprintf("%v;%v", pc[0]+1, pc[1])] = true + } + } + + if pc[1] < len(grid[0])-1 && grid[pc[0]][pc[1]+1] != '#' { + if _, ok := alreadyStepped[fmt.Sprintf("%v;%v", pc[0], pc[1]+1)]; !ok { + nextStepCells = append(nextStepCells, []int{pc[0], pc[1] + 1}) + alreadyStepped[fmt.Sprintf("%v;%v", pc[0], pc[1]+1)] = true + } + } + + } + + possibleCells = nextStepCells + //fmt.Printf("Possible steps: %v\n", possibleCells) + } + + return len(possibleCells) +} diff --git a/2023/21/input-example.txt b/2023/21/input-example.txt new file mode 100755 index 0000000..9e1d842 --- /dev/null +++ b/2023/21/input-example.txt @@ -0,0 +1,11 @@ +........... +.....###.#. +.###.##..#. +..#.#...#.. +....#.#.... +.##..S####. +.##..#...#. +.......##.. +.##.#.####. +.##..##.##. +........... diff --git a/2023/21/input-user.txt b/2023/21/input-user.txt new file mode 100644 index 0000000..7a37da6 --- /dev/null +++ b/2023/21/input-user.txt @@ -0,0 +1,131 @@ +................................................................................................................................... +..#.......#.#.##...........#...#..................#......................#.......#.......#............#...............#.....#...#.. +................#....#.#.....#.#..#.#..#.........#.....................#......................#......#....#..........#............. +..#.##.....................#......##..#...##.#............................#.....#..............#..................#..#......#...... +.....#.#.....#...#........#............#.#.#..#.#...#.........................................##......#..........#.#..##........... +.....#....##.........#.......##............##....#.............................#.........#.....#....#.#....#...........#.##........ +...........#..#...#...........#.....##................#.........#..#...................#.#..#.....#................................ +..#.....#....#....#....#......#.#.............................................###.#.......#....#..........#.#....#.....#........... +....#........................#............#.......#...............#..#.......#..........#.........#....#......#..##.........#...... +..........#...#..........#..#............##.........#...........#...............................#.........#........#........#...... +....#............#.#...........#.............#..#..............................##.#....#...#.#.#.#.#........#......#..#............ +....#..#...#.#....#.#.##..#...................##..#........#.##....................#............#......#........#...#.............. +......#.....#...##...#...#......##.....#.....#.#...............#....................#.#......#.............#..........#.........#.. +......#....#..#...........##...#.#.#.#.#......##...........#.......#.....##.............#.#..##...##....................#.......#.. +......................................#.#......#.......#....#...#.#..#...#............#..#........####......#....##................ +..#........#........#.............#....................#.......#....#.....#................#...................#..........#........ +..................#.....#.#.#....#......#.....................#............#...............#..#......#...#..##......#............#. +...#........#....#......#............................#.......#....#........#.#............................#..#....#......##........ +..#..........#...#..................#......................#......#........#.............#....#..##.#......##......##.#............ +...........#..........#......#..#...#.#...........#.......#.....#.....#..#...............#..............#...#.....#.##.......#..... +.#......#..#....#..................................................#..#......#.........................#.......#.....#.....#.....#. +.....##..................#............................#...#........#..#....#.#..............#.....................##.......#..##... +.........#...#......#.....###..........#...............#.......#..................................#.........##....#.......#........ +....#........#.................#..............#.#......#...##...#....#...#.....#...................#....#...#.....#....#.#......... +.....#....##....#.............#...#..........................#..................#.................#..........#..............##...#. +....#.#..#....#.................#............................#......#.#................................#..##......##.....#......... +......#.................#........#.........#......##....#...#.#........#...#.....#.....................##........#..#..#........... +..........#.#.......#......#.....#..................##.......#........#.....##....#.............#.................#.#..#........... +.#.#....##.#.#.......#...................##....#...#..........#.........#.#..##.#.......#........#....#....#.#..........##....#.... +...............#......#...#..............###......#.............#....#...##........#.................#....#.................##.#... +.......#....#.......#.....##.#................#...........#....................#.....##...##.........#....................##....#.. +.................#.....#........................#........#.....#........#...#......#................#............................#. +..........#.................#........#..#.....................#...........##.#..............#..................#.................#. +.#.#.#................................#...#.#...#.........#...#.....#...........#..........#.#........#.#.#..........#.....#....... +.#..#...#..#...............#.................#...##...#........#........#....####......#....#.............#..#...#.............#.#. +............##........#...............#.#......................#...#..#...##..##..........#...#.........#..........##.......#...... +.........#.#.#...#......#.....................##...............#..#.###........#............#..#......................#......##.... +.#........##.......#.............#.#..........#..#........#..#.#.................................#..................#......#.#..... +..##...#....#..........................#...#.#.#.........#..#............#.......#....#..............................#....#........ +......#.....##....#...................#...................#........#...#....#...........#..#.....#.................#.#..#.......##. +....##...#......................#.##..#....#...#.....#..................#..#...........#.....#................#...##...........##.. +...............##...............#.................#.......##..........................#...#.#.........#........#........#......#... +.##....#....#...#...........#.....##....#..............#...#.........#...#.....##.....#........##.#.#...................#.......#.. +.#............................#.....#...#....#.#.....#.....#.###.............#...............................................#..#.. +..........#..................#...#......#...............#.#.......#............#..........#....#.................#....#.......#.... +.#..#....#...#....................#......#........#.......#...#.#........#.#..........#.............#..##...........#..#........... +....#......#..................#.......#.......###..#.#.#...........#.#............#................#.....#...........##..#.##..#... +.......#.#.#...................#.##...........#.........#...#............#.....##....#..........#.....#.#.............#.#...#...... +............#........##....#...........#.............................#.#.#....#..#......#.................#..........#......#.#.... +.....##.#.........................#.#...##.#..#....#..........#..........#............#.......#..##.....##.##..............#....... +..#..#......................................#.......#.#..#.#.........#.....#...............#.....................................#. +......#........................#.......#.#............................#..#..#.......#......#.....#............#.#................#. +...###.#.........#..##......#............#.............###......#....#....##...................##...#..#.#....#.............#.#.#.. +..##....#...........#.............#..#..#.#..........#..............#...#........#..##..#.........................#.......#........ +.#.....#.............##.....#.....###.#.#..#.......#......#..#........#..............#.#....#.....................#...........#.... +....#.........................#....##......#..#......#.#.....#.#.....#.................#.................#.........#............... +.#............#.........#........#.#.#.....#.....#...#.....#......#......#...............#.#.........#............................. +................##...#...#......#....#....#.........##.............#.......#.....#.......................#..###..#...##............ +...........#.#..............#..#.......#....#.#.....#..............................#..##....................#.#......#............. +..................#....................#............................##.#..#.#..........#..................#............#........... +.............#.....#.......##...#..#........#.................##......#......#.#..............#..........#........#.#.#............ +...........................#....##...#.###...........#.#..........#.............#.....#.#....#.....#....#..........#............... +.............#........##........#.#.#..##....#......#.............#.#....#.#.#...#...#....#.#..#.......#...........##..#........... +......#..#.....#........##.....#......#......#.#....#.#..#...#.........#.........#..............#..........###..................... +.............#..........#..#...####..........#......#..#....#.........#..##............##.....#..#.....##.#......#.#....#..#.#..... +.................................................................S................................................................. +.....................#.....#.#........##......#......##..#..#.......#.##..#...........................#..#......................... +......................#.#..........##....................#.................#......#..#.....................##.....#.......#........ +................#..........#...#.....................#....#..........#.##.....##........#......#........##....#..........##........ +.........##...#.#..#.....#.#...#.#................#.........#.......#..##..#.....#...#...##....#...#............#....##............ +...........................#..#..#...#..#...##..................................................##.........##.#...#................ +............#..#.........#........#.......#......#...#......##.##.................#.#........##.#....#....#.#...#....#............. +...........#..#.....#..#..#......#.#..#............#............#.........#..#.....#..#........#..............##.......#.......##.. +...................#....##...........#.#......#.......##..#..#.#.....#...#...#.##..............#.##....#....#.#.....#.........#.... +..................#..#....#......#.#...........#...#.......#........#...#......#...........#.....................#.#.............#. +................#...........#.............#.#.....#.........................##.#....#..#..........#.#...#....#................##.#. +......##........#.#......#..........#..........#..........................#...#..........###.#..............................##..... +.....#..............#........#.....#.#..#....#........##........#..#......#......#......#..#......#......##...................#.... +.#................#.....#...........#.#.........#.#.....#.##.............#...........#.#.#....##.#..........#...............#.#..#. +..#..#.............#...............#....#..........#..#.....#...#...............##.....#...#............#.#...................#.... +......#............#.....#.................#..............##.#......##............#....#..#.#..#.#.#.............................#. +...#..................##..........#..........#...#.................#.......#.....#...#...................#..................#.#.... +.#....##....................#...#...#.........#.#....#...#.#.......#..##..#.#....#........##.#....##......#..........#...#....#..#. +....#.........#............#..#.....#.#.....#.#..#.....#................#..........#.....##...........................##........... +.#............................#...............#...#.#.........#.....#...#..........#...#.......##..#..................##.##....#... +.......#..................#.#.............#.......#....#............................#...................#.#........................ +.........#........................####......#......##...##............#.....#...#...##.......#.....#.............#.##.#............ +..#..#.##......#..........#..............#............#...................##...................#..#.....#.......#.....#...#...##... +....#..#...........................#.........#...#.........#.#.....#.............#.......#............#.................###.#....#. +...#.......#.#.......................##....#.......................#............#......#.....#..#....#..........#.......#.......... +.......#.........##.#................#.........#.#..#.........#.....#.#...............#.#....#....#............#.....#............. +..................#.#.......................#................##......#...............#......................#..#.#.#..........##... +......##.......#.......#............#.....#........#.....##....#.....#.............#.#....#.....#.............#......#............. +...#.........#.....#...............#.........#.....#..........#...#.........#....#.#.......###..#.#..............#................. +.........#....##.#...#............#.....................#...........##......#..#..##......#.................#..#.##................ +.........#.........#...##..............#.#...#..###........#.#......#...#..#............#.......#.............#.....#.......#....#. +.....................#...#..........#.#............#...#.#.##..##..........#..#........#.......#........#.......#..#.....#.#.....#. +....#..........#....................#...............................#.##.##.......###.........#........#....#.................####. +..........#...#.............##..................#..#....#............#...................#..#.............#.......#...#.......#.... +.#....#..##......#.#.##.....#.............................##.#............#.#..............#.........#.................#......#.... +..#.........#.....#........#................#...#...................#..............#..#............#...#...#...................#... +....#..#..#..#...........#................#......#...#......#.....#........#...#...##.#..............#........#.#......#..#..#.#... +......##..#..#..............##...........#.#...#............#...#.#.....#.#.....#.......##...............#.#....................... +.......#.........#.........#..............#...#......................#....#....#........#.......#.........#......#................. +...#..#.........#...#...#........#...............#............#....###.#.#.........#...............#................#.......#....#. +.......#.....#........#..........#...............................................#..#.#...................#.........#..#........... +................#..#....#..#......................##...............##...#.##...#..................#.....#.......#.................. +.#......#...#...............#.........................#...#.............#.....#............................#.................#..#.. +.................##....##........................#.......#.#...........#......................##.......#.............#...#..#...... +.#..........#....#................##....#.............#.......#...#...#......#...#.................#....#....................#...#. +..................##..#.#.##..#.#...........................#.................##..........#.....................#..........#....#.. +....................#...#............#..#................#...#.#...........#..#............#......##....#.....##..............#.... +.#..#......#........#.........#......#..#................#.#...#..#.#......#.............#.#.....#....###...#.#....##.............. +....#......#.#..#................#.#...#................#...#.......#.#.#..............#......................#..#.....#........#.. +........#.............#...##.#.#...#......#................#........#...#...............#.#.......#........##.#...#.......#......#. +.........................##..#......#.#.....#.........##....#............#..........#.............#......#....#............#...#... +...#.....#..#.....#.........#..#..#.##.................................#.............#......##............#....#..#......#.......#. +..#...#....#.#.#...#...#..#...#............##.............................#..................##..............#...................#. +..#..#.....##.............#...........#...#...#.#..........#.......#....#...........#........#........##.......#.......#........... +........#.#...#...........#..###...........#.....#................#.................##.........#.....#....#....#.....#.#.......#.#. +...#......#..............#....#...##...#..###.....#.........#.......#....................#...#.........##..........#..#..........#. +.....#.#....................#........#....###.#..............####....................................#.#...#..#...........#....#... +...#.#...##...#.........#...#............#.....#...#............#...#.........#..#...........................#..#............#..... +.#...#.#..###..................#...#..........#......#........#.#................#.......................##.###.....#.#............ +.....................#.#..#.....#.#..#............................................#..#.......#...#.#.........##.....#.............. +...#............##.......#...#.##..#........##...#....#....................#.#......#.#....................##.#...#..#....#..#..... +........#....#..........................#...#...........................................#...#..............#..........##.......#... +..............#..#............##......#..#.#......#....#.........................#.....#..#..#...#..........##.....#..........#.... +..........#....##........#....#..#....#.................#.................#.#...............#.......#....#...#...............#..#.. +...............#..#......#..........#...#......#......##.#..............#.#..........#...#......#..#....#................#..#...#.. +...................................................................................................................................