Skip to content

Commit 50a6d06

Browse files
committed
feat(aoc-2023): day 08 part 1 and part 2
1 parent 8c955a4 commit 50a6d06

6 files changed

Lines changed: 846 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.markkovari.adventofcode.day08
2+
3+
type State = String
4+
5+
type Transition = (State, Instr) => State
6+
7+
enum Instr:
8+
case GoLeft, GoRight
9+
10+
object Instr:
11+
def parse(inp: String): LazyList[Instr] =
12+
inp
13+
.map {
14+
case 'L' => Instr.GoLeft
15+
case 'R' => Instr.GoRight
16+
}
17+
.to(LazyList) #::: Instr.parse(inp)
18+
19+
def parseNetwork(inp: List[String]): Map[String, Vector[String]] =
20+
inp.map { case s"$a = ($b, $c)" =>
21+
(a -> Vector(b, c))
22+
}.toMap
23+
24+
def countStepsUntil(
25+
state: State,
26+
instrs: LazyList[Instr],
27+
trans: Transition,
28+
count: Int,
29+
pred: State => Boolean
30+
): Int =
31+
if pred(state) then count
32+
else
33+
countStepsUntil(
34+
trans(state, instrs.head),
35+
instrs.tail,
36+
trans,
37+
count + 1,
38+
pred
39+
)
40+
41+
def transitions(network: Map[String, Vector[String]]): Transition =
42+
(n, d) =>
43+
d match
44+
case Instr.GoLeft => network(n)(0)
45+
case Instr.GoRight => network(n)(1)
46+
47+
def part1(input: String): Int =
48+
val inpL = input.split("\n\n")
49+
val instructions = Instr.parse(inpL.head)
50+
val network = parseNetwork(inpL.tail.head.split("\n").toList)
51+
val trans = transitions(network)
52+
53+
countStepsUntil("AAA", instructions, trans, 0, _ == "ZZZ")
54+
55+
def part2(input: String): Long =
56+
val inpL = input.split("\n\n")
57+
val instructions = Instr.parse(inpL.head)
58+
val network = parseNetwork(inpL.tail.head.split("\n").toList)
59+
val trans = transitions(network)
60+
61+
val starts: Set[State] = network.keySet.filter(_.endsWith("A"))
62+
63+
def leastCommonMultiple(a: Long, b: Long): Long =
64+
a * b / greatestCommonDivisor(a, b)
65+
66+
def greatestCommonDivisor(a: Long, b: Long): Long =
67+
if b == 0 then a else greatestCommonDivisor(b, a % b)
68+
69+
starts
70+
.map(state =>
71+
countStepsUntil(state, instructions, trans, 0, _.endsWith("Z")).toLong
72+
)
73+
.reduce(leastCommonMultiple)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
RL
2+
3+
AAA = (BBB, CCC)
4+
BBB = (DDD, EEE)
5+
CCC = (ZZZ, GGG)
6+
DDD = (DDD, DDD)
7+
EEE = (EEE, EEE)
8+
GGG = (GGG, GGG)
9+
ZZZ = (ZZZ, ZZZ)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
LLR
2+
3+
AAA = (BBB, BBB)
4+
BBB = (AAA, ZZZ)
5+
ZZZ = (ZZZ, ZZZ)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
LR
2+
3+
11A = (11B, XXX)
4+
11B = (XXX, 11Z)
5+
11Z = (11B, XXX)
6+
22A = (22B, XXX)
7+
22B = (22C, 22C)
8+
22C = (22Z, 22Z)
9+
22Z = (22B, 22B)
10+
XXX = (XXX, XXX)

0 commit comments

Comments
 (0)