commit f6823d927c5a160a5db961ab01269cfa3b482a60 parent 0d618af316e2b6af81063c5ec4d3763ffbfc2c14 Author: Martin Ashby <martin@ashbysoft.com> Date: Fri, 13 Dec 2024 19:08:43 +0000 Day6 pt1 Diffstat:
A | day6.txt | | | 131 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | day6_sample.txt | | | 11 | +++++++++++ |
A | src/main/scala/Day6.scala | | | 38 | ++++++++++++++++++++++++++++++++++++++ |
M | src/main/scala/Main.scala | | | 4 | ++-- |
4 files changed, 182 insertions(+), 2 deletions(-)
diff --git a/day6.txt b/day6.txt @@ -0,0 +1,130 @@ +..#.........#...#......#...........#.#...#............#...#.........#....#......................#......#.......................... +...........#....#.............#.....................................#............................................#.....#.......... +.................#.........#.......#.......#..#............#.........#.........#............................................#..... +......#................................#.........................................#................................................ +#...............................##......#.#...........................#....................#...#...............#.................# +.............#......#...........................................#..............#..........#..............#........................ +...#...................#..............#........#.......#..........#.........................#.#.....#.....#....................... +#.#...#.............................#.................................................#..............#............#............... +.............................#.......#.............#......#...............#.....#................................................. +....................................#.....................................#.................#........#..........#.#..#...........# +......................#......#...............................#.................#.......#......................#.......#........... +...#..............#....................#............#...........##..#.....................................................#....... +....#.........#...................................#................................................#..........................#... +..............#.....#.................................................................#..#.....##.......#......................... +.........#....#...........##..........................................#......#.....................#..........#.......#........... +..#....#..........#.................................................................#....#............#............#........#..... +............#.#....#...........#.............#...........#..................#.....#.................................#.......#..... +..........#.......#............................#........#...........#.......#...#....#........#.............##.#............#..... +...................#.........#....#.....................................................................#.#..........#............ +.#...#.#..................#...............................................#...........................#......................#.... +.........#..............#.........#.......#.#......##..................#......#...#..#...........#.....................#.......... +...............................#.............#..#......#............#..........#......................#..........#.............#.. +..........#.....#..........................#..................................................#.#..#.....#........................ +............#..##.................................#....................#.....#.......................................#............ +......................#.#...................#......#..............................................................#............... +.......#.............................................................................#............................................ +........#.................#....................................................#.......................................#....##.#.. +..................#........................#......#..##....#.........#...##..#....................................#............... +.............#..............................................................................##...#......#.................#....... +..................................#.#.............................................................................#............... +...........#.................................#.......................................................................#......#..... +.....#............................................................#................................................#.............. +.....#...........#.#..........#..............#..........................#.................................................#....... +....#........#...#..........#..#..........................................................................#....................... +.........................................................................#.....#.......................................#....#..... +...............#...#..#.....#.......................#.................#........#..........##....................................#. +.......................#....#...#..................#..................#.....................................#.............#....... +...........#..#...#..............................................^........#........................#......................#....... +.............................................#.................................................................................#.. +......#...#.........#.....................................#...............................................#..#....#.......#....... +...#......................................................#.................#..#.............................................#.... +.....#......................#.......................................#.............................................#....#.......... +...#.....................................................#......#...#.......#....#..............................#....#............ +......#.....#.....#...........#...#................#...........................................#.................................. +...................................#...............#......................................................#....................... +..............................#...#........................................................#.#.............#...................... +......#.......#............#..#......................#..#.....#..................#......#......................................... +#......#....#.......#....................#.....#.#............................................................................#... +.#....#...................................#.................#....................#........................................#....... +............................................#...................................#.......#..............#.........................# +............##.........#..............................................................................#.........#................. +##.................................................#.......................#.#.....#.............................................. +...........................#..........#...#.#.........................#.................................##.....#.................. +.............#..........#........................................#............#............#.....................................# +.....................................................#.................................................#.......................... +.....#........................................#.............................#...#.....................#........................... +.......#..................................#...........#.......#.....#........................................#.......#............ +.............#.......................#......................#.....#.......#..................................#..#........#........ +.......................................................#......#...............#.........#.............#............#.............. +....#...............................#.#.....................#.................................................................#..# +...........................#........#..........................................#........................................#......... +....#............................................................................................................................. +.##........#.......#..................#.#....#.............................................................#.##................... +.#..#.......................##........................................................#...............................#........... +.....#.#..............#...........................................#............................................................... +.....#.#............................#............................................................................................. +......................#............................#....#............................................................#............ +.......#.......................................................................#..................#...##.......................... +...............#...................................................#.....................................#...............#.#...... +....#..........#.......#..#................................#...................................................................... +#......##..................................................................#...#........................#......................... +..................................#........................................................................#...........#......#... +.#....................#....#..................................#..#..................#.#.......................#................... +......#..#................................................................................................##...................... +.....##........#..#.......................#.........#............#....#........................................................... +...............#...........#.................#...........#.........#.......................#.........#........................#... +............................................................................................#..#.................................. +..........................................#.............#..#...............#.............................#.#......#.............#. +......................#............#..........#............................................................#...#................#. +...........#...................#.................................................................................................. +.......#........................#.................................................................#.................#............. +#...##................#......................................................................#................................#... +...#.....#.......................#...#..............#....................#.........#.........#.................................... +....#.....................................#.............................................................................#......... +.........#..............................................................................#..............#.......................... +...................................#.#...............#........................................................................##.. +.........#............#..............#..........................................................................#...#............. +.....#.......................#..#....#.#....................#..........................................#.......................... +.......................................#........#.........#.....................#.............#...........#...............#....... +................#................................................................................................................. +.......#....................................#..................#...........#.....#.........................#...................... +...................#................................................................#...............#................#............ +..........#.......................#......................#...........#..#........#....#.........................#................. +...................#........................#.......#................................................#............................ +.......................#......................................................#...........#.......#...................#........... +........#..........#........#......#......................#......................................#...............#................ +...........................................................#...#.................................................................. +.......................#......................#............#..#....#...#............................#............................. +.#.#...............#..........#..........##........#.............................................................................. +##.........#....................#...................#.........#......................##..................#...............#........ +.#......................#...#...#......................#............#......................#.................#..................#. +#..............................#......................................#.........#.#............................................... +...........#....#......#.......#..........................#..........................................#......#............#..#..... +................................................................................................#.......#......................... +....#..................................#..........................#.................................#...#...#..................... +.......................#.#..#.............................#.#...........................#......#..........#....................... +...##.......................................#..................#....................#............................................. +...#.......#........................#..........##.....#............#......................#.....................................#. +......#..............#.............................#.............................................#.............................#.. +#...........................................#.......#....#.#..........#........#............#.......................#.......#..... +..................#.......#............#.....#.............................................................#...................... +.#.....................................................................................#...................................#...... +...........#.........#.#.................#..........................#............................................#.......##....... +.......................#......#.................................................#........................#.................#...... +..................#........................................................#................................................#....# +...............................#.......#....#.........#..........#.....#.......................................#..#............... +......##................##...#....#...#...#............................................................................#.......... +.......................#............#...................................#....#.......#.............................#.............. +..............................#.........................................................#........#.............#.................. +........................#............................................#..................#.....#...................#............... +............#......#...............................#.............................................................................# +.#.........#.........#.........#.......#.#..#...#...............................#.......#.........#..........................#.... +........................#.................................................#....................................##......#.......... +....#.......#.....................##...#.......................................................................................... +.................................#.......................................#.......................#.#......#....................... +.#.............#..................#...........................#.................#...........................................#..... +.....#.........#................#..........#..#..........#......................#...#......................#....#................. +.................#......#.....................................................................#................................... +......................#.......................................................#..#......#..............#..#..#..........#......... +...............................#.........#........#..................#.#.........#.#.#........#..#.........#....##...#............ +\ No newline at end of file diff --git a/day6_sample.txt b/day6_sample.txt @@ -0,0 +1,10 @@ +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#... +\ No newline at end of file diff --git a/src/main/scala/Day6.scala b/src/main/scala/Day6.scala @@ -0,0 +1,37 @@ +import scala.annotation.tailrec +def day6_pt1(input: Iterator[String]): Int = + val lst = input.toList + val pos = lst.zipWithIndex.find((s, y) => s.contains("^")).map((s, y) => (s.indexOf('^'), y)).get + val grid = updated(lst, pos, '.') + do_follow(grid, pos, '^') + + +def updated(grid: List[String], pos: (Int,Int), replacement: Char): List[String] = + val (x, y) = pos + return grid.updated(y, grid(y).updated(x, replacement)) + +// def print(grid: List[String]): Unit = +// grid.foreach(line => println(line)) + +@tailrec() +def do_follow(grid: List[String], pos: (Int,Int), dir: Char): Int = + val grid2 = updated(grid, pos, 'X') + val pos2 = dir match + case '^' => (pos(0), pos(1)-1) + case '>' => (pos(0)+1, pos(1)) + case 'v' => (pos(0), pos(1)+1) + case '<' => (pos(0)-1, pos(1)) + val (x2, y2) = pos2 + if (x2 < 0 || y2 < 0 || x2 >= grid(0).size || y2 >= grid.size) { + return grid2.map(line => line.count(ch => ch == 'X')).sum + } + if (grid2(pos2(1)).charAt(pos2(0)) == '#') { + val dir2 = dir match + case '^' => '>' + case '>' => 'v' + case 'v' => '<' + case '<' => '^' + do_follow(grid2, pos, dir2) + } else { + do_follow(grid2, pos2, dir) + } +\ No newline at end of file diff --git a/src/main/scala/Main.scala b/src/main/scala/Main.scala @@ -1,2 +1,2 @@ @main def run(): Unit = - println(day5_pt2(scala.io.Source.fromFile("day5.txt").getLines())) -\ No newline at end of file + println(day6_pt1(scala.io.Source.fromFile("day6.txt").getLines())) +\ No newline at end of file