From 4d9aa6e0182b3ea1b5352f6b8c23243e3ea278f9 Mon Sep 17 00:00:00 2001 From: Paul-Henri Froidmont Date: Mon, 8 Dec 2025 01:39:53 +0100 Subject: [PATCH] Day 7 --- input/day07 | 142 ++++++++++++++++++++++++++++++++++++++++++++++++ src/day07.scala | 46 ++++++++++++++++ 2 files changed, 188 insertions(+) create mode 100644 input/day07 create mode 100644 src/day07.scala diff --git a/input/day07 b/input/day07 new file mode 100644 index 0000000..2c60adb --- /dev/null +++ b/input/day07 @@ -0,0 +1,142 @@ +......................................................................S...................................................................... +............................................................................................................................................. +......................................................................^...................................................................... +............................................................................................................................................. +.....................................................................^.^..................................................................... +............................................................................................................................................. +....................................................................^...^.................................................................... +............................................................................................................................................. +...................................................................^.^...^................................................................... +............................................................................................................................................. +..................................................................^...^...^.................................................................. +............................................................................................................................................. +.................................................................^.....^.^.^................................................................. +............................................................................................................................................. +................................................................^.^.^.^...^.^................................................................ +............................................................................................................................................. +...............................................................^.^.^.^.^.....^............................................................... +............................................................................................................................................. +..............................................................^.^.^.^.....^.^.^.............................................................. +............................................................................................................................................. +.............................................................^.^...^...^.^...^.^............................................................. +............................................................................................................................................. +............................................................^.^.^.^.^.^...^.^.^.^............................................................ +............................................................................................................................................. +...........................................................^...^.^.......^.^...^.^........................................................... +............................................................................................................................................. +..........................................................^.^...^.^.^.^.^...^.^...^.......................................................... +............................................................................................................................................. +.........................................................^.^.^...^.^.^.^.^.^.^.^...^......................................................... +............................................................................................................................................. +........................................................^.^.^.........^.^.^...^.....^........................................................ +............................................................................................................................................. +.......................................................^.^.^...^...^.....^...^.^...^.^....................................................... +............................................................................................................................................. +......................................................^.^.......^.......^.......^.....^...................................................... +............................................................................................................................................. +.....................................................^.^.^.....^.^.^.....^.^.^.^.^...^.^..................................................... +............................................................................................................................................. +....................................................^.^...^...^.^.^.^.^...^.....^.......^.................................................... +............................................................................................................................................. +...................................................^.^.^.^.^.^...^...^.^.^.^.^.^.....^...^................................................... +............................................................................................................................................. +..................................................^.^.^.^...^.^...^...^.^.....^.^...^.....^.................................................. +............................................................................................................................................. +.................................................^.^.......^.^.....^...^.^...^.^...^.^...^.^................................................. +............................................................................................................................................. +................................................^...^.^.^.^.....^.^.^.^.^.^.^.....^.........^................................................ +............................................................................................................................................. +...............................................^.......^.^...^.^...^.^.^.^.^...^.^...^.^.^.^.^............................................... +............................................................................................................................................. +..............................................^...^.^...^...^.^.......^.^...^.^.^.^.^.....^.^.^.............................................. +............................................................................................................................................. +.............................................^...^.^.^...^.....^...^...^.^...^...^.^.......^.^.^............................................. +............................................................................................................................................. +............................................^.^.^...^.^...^...^.^.....^.^.^.^.^.^.^.^.^.^.^.^.^.^............................................ +............................................................................................................................................. +...........................................^.^.^...^.^.....^.^.^...^.......^.^.^.^.^.....^...^.^.^........................................... +............................................................................................................................................. +..........................................^.^.^.^.^.^.^...^.^.^.^.^.^.^...^.^.^.^.^.^.....^.^.^.^.^.......................................... +............................................................................................................................................. +.........................................^.....^.^.^...^.^.^.....^.^...^.^...^.^.^.^...^.^.^.^.^...^......................................... +............................................................................................................................................. +........................................^.^...^.......^.^.....^.^.^.^.^.^.^.^.^...^.^.^.^.^.^...^.^.^........................................ +............................................................................................................................................. +.......................................^...^.....^.......^.^.^...^.^.^.^.^.....^...^.^.^.^.^.^.^.^.^.^....................................... +............................................................................................................................................. +......................................^.........^...^.^...^.^.^.^.^...^...^.^.^.^.^...^.^.^.....^...^.^...................................... +............................................................................................................................................. +.....................................^.^.....^.^.^.^.^.^.^.^.^...^.......^.^.^.....^.......^.^.......^.^..................................... +............................................................................................................................................. +....................................^.^...^.....^.^.^...^.....^.^.^.^.^.^...^.^...^.....^.^.^.^.......^.^.................................... +............................................................................................................................................. +...................................^.^.^.^...^.^.^.^.^.^.^.^.^.....^.^.^.^.....^.^.....^.......^.^.^...^.^................................... +............................................................................................................................................. +..................................^.^.^.^...^.^.^.^.^.^.^.^.^.^.....^.....^.^...^.^.^.....^.^.^.^.^.^...^.^.................................. +............................................................................................................................................. +.................................^...^.^.^...^...^...^.^.^.^.^.....^.^.^...^.^.....^.^.^.^.....^.^.^.^.^.^.^................................. +............................................................................................................................................. +................................^.^.^...^.........^.^.^...^.^.^.^...^.^.^.^.^.^...^...^.^.^.^.^.^.^.^.^...^.^................................ +............................................................................................................................................. +...............................^.^.^.^.^.^.....^.^.^.^...^...........^.....^.....^.^.......^.^.^.....^.^.^...^............................... +............................................................................................................................................. +..............................^.^.^.^.^.^.^...^.^.^.....^.^.....^...^...^.^.^.^...^.^...^.^...^.......^...^.^.^.............................. +............................................................................................................................................. +.............................^...^.^.....^.^.^.....^.^...^.^...^.^...^...^.^...^.^...^...^...^.^.^.....^.....^.^............................. +............................................................................................................................................. +............................^.^.....^.^.^.....^.^...^.^...^.....^.^...^...^.^.^...^...^.^.^...^.^.^.^.^.^...^...^............................ +............................................................................................................................................. +...........................^...^.^.^...^.^.^.......^.^.^...^.^.^.^.^...^...^.^.^...^.^.^.^.^.^.^.^.^.^...^...^...^........................... +............................................................................................................................................. +..........................^.^.^.^.^...^.^.^.....^.^...^.....^...^...^...^.^.^.....^...^.........^.^.^...^.^.^.^.^.^.......................... +............................................................................................................................................. +.........................^...^.^.......^...^.......^...^.....^.^.^...^.....^...^...^...^.^...^.^.^.^...^.....^...^.^......................... +............................................................................................................................................. +........................^...^...^.^...^.^...^.^.^.^...^...^.^.^...^...^.^.^...^.....^...^.^.^.^.^...^.^.^.^.^...^...^........................ +............................................................................................................................................. +.......................^...^.^...^...^.^.^.^...^.^...^.....^.^.^.^.^...^.^.^.^.^.........^.^.^.^.^.^.^.^...^.^...^...^....................... +............................................................................................................................................. +......................^...^.^.^.^.^.^.^.^.^.^...^.^...^...^.^...^...^.......^...^.....^.^.^...^.^.^.^.^.^.^.^.^.^.^.^.^...................... +............................................................................................................................................. +.....................^...^.....^.^...^.^.^...^.^.^.^.^.^.^.^...^...^.^.^.^...........^.^.......^.....^.......^...^.^.^.^..................... +............................................................................................................................................. +....................^.^.^...^.^...^.^.....^.^.^.........^...^.^...^.^.....^.^.^.....^.......^.^.^...^.^.^.^.......^.^.^.^.................... +............................................................................................................................................. +...................^.^.^.^.....^.^.^...^.^.^...^.^.^.^.^.^.^.^.^.......^.^.^.^.^.^...^.^.^.^...^.^.^...^.^...^...^.^.^.^.^................... +............................................................................................................................................. +..................^.....^.^...^.^.^.^.^.^.^...^.^.........^.^.^.^.^...^.^...^.......^.^.^.^.^.....^.^.^.^.^.^.^.^.^...^.^.^.................. +............................................................................................................................................. +.................^...^.^.^.^.^.^.^...^.^...^.^.^...^.^...^.^.^...^.^...^...^.^...^.^...^.^...^...^.....^.^.....^.....^...^.^................. +............................................................................................................................................. +................^.....^.......^.^.^.^.^...^.^...^.^.^.^.^.....^.^...^.^.^.......^.^.^.^.^.^.^.^.^.^.^.^...^.^.^.^.^...^.^.^.^................ +............................................................................................................................................. +...............^.....^.^...^.^.^.^.^...^.........^...^.^.^.^...^.^.^.^.^...^...^.^.^.^...^.^.^.^.^.^.^...^.^.....^.^.^.^.^.^.^............... +............................................................................................................................................. +..............^.^.^...^...^.^.^.^.^.^.^.^.^.^.^.^.^.^.....^.^.....^.^.^.^.^...^.^.^.^.^.^.^.^...^.^.^.^.^.^.^.^...^...^...^.^.^.............. +............................................................................................................................................. +.............^.^.^.^.....^.^.^...^.....^.^.^.......^...^.^.^.^.^.^.^.^.^.^.^.^.^.^.....^.^.^...^...^.^.......^...^...^.^.^...^.^............. +............................................................................................................................................. +............^.^.^.....^.^.^.^.^...^.^.^...^.^...^.^.^.^.^.....^.^...^.^.^.^...^.......^...^...^.^.^.^.^...^.^.^.^.^.^.^.^.^...^.^............ +............................................................................................................................................. +...........^.^...^.^...^.^.^...^.^.^.^.^.^.....^...^.^.....^...^...^.^.^.^...^.^.^.^.^...^.....^...^.^...^.....^.....^...^...^.^.^........... +............................................................................................................................................. +..........^.^.^.^.^.......^.....^...^...^...^...^...^.....^.^.^.^.^.....^.^...^.^.^.^.^.....^.^...^.^.^.^.....^.^.^.^.^.^...^...^.^.......... +............................................................................................................................................. +.........^.^.^...^.^.^.^.^.^.^.^.^.........^.^...^...^.....^.^.^.^.^.^.^.^.^.......^.^.^.^.^.^...^...^.^...^.^.^.^.^.^...^.^.^...^.^......... +............................................................................................................................................. +........^.^.....^.^.^.....^.^.^...^.^...^.^.^.....^...^.^.^...^.^.^.^.^.^.....^.....^.^.^.^...^.^...^.^.....^.....^...^.^.....^.^...^........ +............................................................................................................................................. +.......^...^.^.......^.^...^.^...^.^...^.....^.^.^.^.^.^.^.^...^.^.^...^...^.^.^.^.^...^...^...^.^.^...^.^...^.^.^.^.^.^.^.^.^...^.^.^....... +............................................................................................................................................. +......^.......^.....^...^...^.^.....^.^...^.....^.^.^.^.^.^...^...^...^.^...^...^.........^.^...^.......^.^.^.^...^.....^.^.^.^.....^.^...... +............................................................................................................................................. +.....^.^...^.^.^.....^...^.^...^.^...^.^.^.^.^.^.^.^.^.....^...^.......^.....^.^.^...^...^.^.^...^.^.^.......^.^.^.^...^...^.^.^.^.^.^.^..... +............................................................................................................................................. +....^.^.^.....^.^.^.^.^.^.^.....^.^.^.^.....^...^.........^.^.........^.^.^.^.^.^.^...^.^.^.^...^.^.^...^.^.^.....^...^.......^.^.^.^...^.... +............................................................................................................................................. +...^.^.....^.^...^.^.^.^.^.^.^.....^.^.......^...^...^...^.^...^...^.^.^.^.^.^.^.^...^.^.^.^.^...^.^.^.^.....^.^.^.^.^.^.^.^.^.^.^...^...^... +............................................................................................................................................. +..^.^.^.^...^...^.......^...^.^...^.^.....^...^.^.^...^...^.....^...^.....^.^.^.^.^...^.^.^.^.^.^.^.^...^.^.^.^.^.^.^.^...^.^.....^.^...^.^.. +............................................................................................................................................. +.^.^.^.....^.........^.^.......^.^.^.^...^.^.^.^.........^.^.^.......^.^.......^.^.^.^.^.^.^...^...^...^.^.^...^.^.^.....^.^.^...^.^.^.^.^.^. +............................................................................................................................................. diff --git a/src/day07.scala b/src/day07.scala new file mode 100644 index 0000000..4e0ba56 --- /dev/null +++ b/src/day07.scala @@ -0,0 +1,46 @@ +package aoc +package day07 + +val dayNumber = "07" + +@main def part1: Unit = + println(part1(loadInput(dayNumber))) + +@main def part2: Unit = + println(part2(loadInput(dayNumber))) + +def part1(input: String): Long = + val lines = input.split('\n') + + def projectBeam(acc: (beams: Set[Int], count: Int), line: String): (Set[Int], Int) = + ( + acc.beams.flatMap { beam => + if line(beam) == '^' then List(beam - 1, beam + 1) + else List(beam) + }, + acc.count + acc.beams.count(line(_) == '^') + ) + + val (_, splitCount) = lines.tail.foldLeft(Set(lines.head.indexOf('S')) -> 0)(projectBeam) + splitCount + +def part2(input: String): Long = + val lines = input.split('\n').toList + + final case class Timeline(beam: Int, paths: Long) + + def projectBeam(timelines: List[Timeline], line: String): List[Timeline] = + timelines + .flatMap(t => + if line(t.beam) == '^' then + List( + Timeline(t.beam - 1, t.paths), + Timeline(t.beam + 1, t.paths) + ) + else List(t) + ).groupMapReduce(_.beam)(_.paths)(_ + _) + .map(Timeline(_, _)) + .toList + + val timelines = lines.tail.foldLeft(List(Timeline(lines.head.indexOf('S'), 1)))(projectBeam) + timelines.map(_.paths).sum