From 22c66a47574d7108fbdc3aa9b37804daeb543aa7 Mon Sep 17 00:00:00 2001 From: Paul-Henri Froidmont Date: Sat, 7 Dec 2024 13:32:08 +0100 Subject: [PATCH] Day 06 --- input/day06 | 130 ++++++++++++++++++++++++++++++++++++++++++++++++ src/day06.scala | 72 +++++++++++++++++++++++++++ 2 files changed, 202 insertions(+) create mode 100644 input/day06 create mode 100644 src/day06.scala diff --git a/input/day06 b/input/day06 new file mode 100644 index 0000000..e2b0649 --- /dev/null +++ b/input/day06 @@ -0,0 +1,130 @@ +......#........#..........#.................##......................#.............#..................#............#............#.. +.....#............................................#.......................#.......##.................#..#...................#.#... +......#...................#............................#.....#.........#................................................#......... +...........................................#.......#..........#..#......#.........#.#.#.#................#.##........##........... +........#.........................................#.......#.....#..........#...#....##....#..#.......##................#......#... +...................#..#.....................................................#.................................................#... +....#..................#..........#........#.............#................................................................#....... +......#..............#..................#...........#......#.....#...........................#..#.#.....................#.....#... +......#...#...............................................#.....#.........................#..........#............................ +......#......#..........................................#..................................#....#..#................#..##...#..... +........................................................................................#......................................... +................#..........#..........................#.##......................#..........#...................................... +.............#.............................#......#..............................#....................#...........#.....#...#..... +.......#.................#..................#..........................#...........................................#.............. +.......#.....................................................#..........................................#....#.#.....#......#..... +...#......#............#........#......................#...........................#.#....#..#....................#.............## +...........................##..#..#..........#....................#................................................#......#....... +.....................#...#.......................#...#..#.....#................#...........................................#....#. +.................#................#.............#...#...........................................................#..#.#............ +#.....................#....................##.......#...................#..........#..........................#................... +.#..........##......#...#......#................#.....................................#.#.....#..............#.................... +...#...#..#..........................................................#..#..#...................................................... +..................#.#..............#...#...............................#...#....#.#.........#.........................##.......... +.......#.........................#..............................................#........#....#....#.........#.................... +...........#......#......#...........#........#.............................##.................................................#.. +............................#...................................#......#........................................#................. +......................................##...........................................#.............#..#...........#...#.....#....... +...................................................#.................................#............................................ +..........................#..........................................#..#.....#.................#.#......#........................ +..................#...........#.............................#....#.#..................#................................#.......... +...................#....#..#.........................................................#....#....................................... +..#..........................................................##..................................#......................#....#.#.. +#.........................#.....#...............................#........................#................#....................... +......................#......#...#..........#......#..............................#...#......#.................................#.. +...............#.....#............................................................................#............................... +#.....................#........#.#........................................................#.......................#.............#. +.......#...#..........#........#...........#.................#..................#....................................##.....#..... +.#......#..................................#.......................................................................#.............. +.................#.....#...........................................................#.............................................. +.........#....................................^......#..................#..............................#..#.............#...#..... +.............................................................#.....#....................................#......................... +....................#.................#........#........................................................#..#............#........# +.....#..........................................................#...........................#.................#....##............. +..............#.........#.................#............................#.............#......................#.#............#...... +.............#.................#.........................................#...#................#.............#.....#............... +....#.....##..#......#........#............................................#..........#.......#.................................## +..........................................................................#....................................................... +.......#..........#.............................................................................................##..#............. +............................#...............#...............................................................................#..... +....#......#...............................................................................................................#...... +..................................#......................#.....#............................................#.....#........#...... +...............................#..........................................#....................................................... +.....#.............#...#..............#.#............#....................................................................#....... +..............................................#...............................#......##.......#................#..........#.#..... +........#.....##...............................................#.........................#........................................ +.....#.........................#.#........#.............................................#.............#..............##........... +........................................#..##.........................#............#...........#.............#.................... +.....................#.#................#............................#.................#.....#..............................#..... +................#.....................................#..#....................#..................................#................ +...........#...................#.................#..........................................................#.......#...........#. +...........................#..................................#......#..............#............#.........#....#................# +..................................................................................................................#............... +..............#....#.#............................................................................#.....#.........#........#...... +..#...........#.................#.#......#................................#..........................#...........................# +............#...........##...............................#..................................................#..................... +..............##.#..................................................................................#.........#......#......#..... +................................................................#...............#....#....##................#..................... +.........#..#.....................#........................................................#......#................#.............. +...#..........#..........#..................#.......................#........................#...#................#.........#..... +...................................#..#...............................#..........#................................................ +.........................#........................................................................................................ +......................#.........#....................................................##............................#.....#........ +.#....#.....................#.........................................................#....#........#............................. +..........................................#....................................#........................................#......... +.................................................#.......................................#........................................ +#..#...............#......................#....#.............................................#................#........##......... +.........#....#........##.........#.....#.........#...............................................#..................#....#...#... +..#............................................................................#...............................................#.. +.......................................................#.........#......#..........................#.#............................ +.........#.............#.................##.....#.....#.........#............#.........#..................#.....#.............#... +#..........#.........#...............................................................................................#............ +.......................#....................#..................................................#.................................. +.#................#.................................#........................................#...........#........................ +.........#........................................................................................................................ +..................................................................................#..........#.....................#.............. +..#...............................#.......#.....................#......................#.....#..........................#......... +......#....#..........................................................#..........................#................................ +...........................................##..................................................##..................#...#.......... +...............#....#...........................#........................................#..............#....................##... +#......#......................................................#..........#.........................#.........#.................... +...#...................................#......#..........................................#.........................#.............# +#.....#.........#.............#.......................................#...............................#........................... +...................#.#..........................................................................#...............#................. +.......#......##..........#....................................................#..................#..........#.................... +.............................................#.##..............#......................#......#..#..................#..........#... +..##...................#...................................#.........................................#...........#................ +...................................................#.....................................#.......................#...............# +..........#.......##...................#...............#...............................................................#.......... +.......................#............#............##.............................................#......##.................#...#... +.........#.................................................................................#............#....................#.... +.......##.........#..................................................................#................#........................... +................#.............................................................................................#................... +......................#.......#....#.................................................#......#.#...............................#... +.........#.........#.#.....##.#..................................##...................................................#........... +.#......................................................#.......#......##........#..........#.........#....#...................... +........#.#......................#................................................................................................ +..................#.............................#.................#..................#......................#......##.#........... +................##.............................................#....#..........##.#.#....#........................................ +..............#...................#.......#............................#.#.....................................................#.. +............#..............................................#....#.....................#...#.......................#.....#......#.. +..............#........................................................#............................................#............. +.......#.....#.........#..............#..............................#.......................#..#................#....#..#........ +.......................#......#........................................#.................#..........#............................. +...........#..................#.............................#............................................................#........ +......#....#......#.....................#.....................#........#.....................#...#.....#.#...................#.... +#..............................................##...#.....#...........#..........................................#................ +..........#..........#.#........................................#..#....#......................................................... +..................#......................................#...#.......#............................................................ +...#.............................#..................................................#...........#................................. +................................##.#.............#........................#...#...............................................#... +...#...................................#.......................#........................#............#...............#....#.#..... +..............................................................##.......#...............#.........#..................#............. +....#............................#..................................#..#..#...............#..............##....................... +#...#.................##...............................#...........#...........#..............#.#.........#........#...#.......... +..........................#.....#....................#..............#.............#..........#....................#.........#..... +......#.......................................................#........#.....#..........................#...#.........#........... +...............................#............#.....#...........................#...............#........#.......................... +............#..#......#...#..#.....................#...............#.........#...........................................#.....#.. +.......#..#..................#............#...........#..............................................................#............ +....#......#.##..#......##..........#.......#............#...............#....#....................................#..........#.#. diff --git a/src/day06.scala b/src/day06.scala new file mode 100644 index 0000000..239fce6 --- /dev/null +++ b/src/day06.scala @@ -0,0 +1,72 @@ +package aoc +package day06 + +import scala.annotation.tailrec + +val dayNumber = "06" + +@main def part1: Unit = + println(part1(loadInput(dayNumber))) + +@main def part2: Unit = + println(part2(loadInput(dayNumber))) + +def part1(input: String): String = + val (gridMap, guardPos) = parseInput(input) + walk(guardPos, Dir.Up, Set())(using gridMap).size.toString + +def part2(input: String): String = + val (gridMap, guardPos) = parseInput(input) + (walk(guardPos, Dir.Up, Set())(using gridMap) - guardPos) + .count((x, y) => + isLoop(guardPos, Dir.Up, Set())(using gridMap + ((x -> y, '#'))) + ) + .toString + +def parseInput(input: String): (Map[(Int, Int), Char], (Int, Int)) = + val grid = input.split("\n") + val coords = + for + y <- 0 until grid.length + x <- 0 until grid.head.length + yield (x, y) -> grid(y)(x) + val gridMap = coords.toMap + val guardPos = coords.collectFirst { case (x, y) -> '^' => (x, y) }.get + (gridMap, guardPos) + +enum Dir: + case Up, Right, Down, Left + def next = this match + case Up => Right + case Right => Down + case Down => Left + case Left => Up + def move(x: Int, y: Int) = this match + case Up => (x, y - 1) + case Right => (x + 1, y) + case Down => (x, y + 1) + case Left => (x - 1, y) + +@tailrec def walk(pos: (Int, Int), dir: Dir, visited: Set[(Int, Int)])(using + gridMap: Map[(Int, Int), Char] +): Set[(Int, Int)] = + val nextPos = dir.move.tupled(pos) + gridMap.get(nextPos) match + case Some('.' | '^') => + walk(nextPos, dir, visited + pos) + case Some('#') => walk(pos, dir.next, visited) + case Some(_) => ??? + case None => visited + pos + +@tailrec def isLoop(pos: (Int, Int), dir: Dir, visited: Set[(Int, Int, Dir)])( + using gridMap: Map[(Int, Int), Char] +): Boolean = + val nextPos = dir.move.tupled(pos) + if visited.contains(nextPos :* dir) then true + else + gridMap.get(nextPos) match + case Some('.' | '^') => + isLoop(nextPos, dir, visited + (pos :* dir)) + case Some('#') => isLoop(pos, dir.next, visited) + case Some(_) => ??? + case None => false