Day 10 part 1

This commit is contained in:
Wesley Moore 2022-12-11 10:18:59 +10:00
parent ca1892ceff
commit 301a86bd86
No known key found for this signature in database
4 changed files with 392 additions and 0 deletions

138
2022/input/day_10.txt Normal file
View file

@ -0,0 +1,138 @@
addx 1
noop
addx 4
noop
noop
addx 7
noop
noop
noop
addx 3
noop
noop
addx 5
addx -1
addx 1
addx 5
addx 3
noop
addx 3
noop
addx -1
noop
addx 3
addx 5
addx -38
addx 7
addx 10
addx -14
addx 5
addx 30
addx -25
noop
addx 2
addx 3
addx -2
addx 2
addx 5
addx 2
addx 2
addx -21
addx 22
addx 5
addx 2
addx 3
noop
addx -39
addx 1
noop
noop
addx 3
addx 5
addx 4
addx -5
addx 4
addx 4
noop
addx -9
addx 12
addx 5
addx 2
addx -1
addx 6
addx -2
noop
addx 3
addx 3
addx 2
addx -37
addx 39
addx -33
addx -1
addx 1
addx 8
noop
noop
noop
addx 2
addx 20
addx -19
addx 4
noop
noop
noop
addx 3
addx 2
addx 5
noop
addx 1
addx 4
addx -21
addx 22
addx -38
noop
noop
addx 7
addx 32
addx -27
noop
addx 3
addx -2
addx 2
addx 5
addx 2
addx 2
addx 3
addx -2
addx 2
noop
addx 3
addx 5
addx 2
addx 3
noop
addx -39
addx 2
noop
addx 4
addx 8
addx -8
addx 6
addx -1
noop
addx 5
noop
noop
noop
addx 3
addx 5
addx 2
addx -11
addx 12
addx 2
noop
addx 3
addx 2
addx 5
addx -6
noop

View file

@ -0,0 +1,146 @@
addx 15
addx -11
addx 6
addx -3
addx 5
addx -1
addx -8
addx 13
addx 4
noop
addx -1
addx 5
addx -1
addx 5
addx -1
addx 5
addx -1
addx 5
addx -1
addx -35
addx 1
addx 24
addx -19
addx 1
addx 16
addx -11
noop
noop
addx 21
addx -15
noop
noop
addx -3
addx 9
addx 1
addx -3
addx 8
addx 1
addx 5
noop
noop
noop
noop
noop
addx -36
noop
addx 1
addx 7
noop
noop
noop
addx 2
addx 6
noop
noop
noop
noop
noop
addx 1
noop
noop
addx 7
addx 1
noop
addx -13
addx 13
addx 7
noop
addx 1
addx -33
noop
noop
noop
addx 2
noop
noop
noop
addx 8
noop
addx -1
addx 2
addx 1
noop
addx 17
addx -9
addx 1
addx 1
addx -3
addx 11
noop
noop
addx 1
noop
addx 1
noop
noop
addx -13
addx -19
addx 1
addx 3
addx 26
addx -30
addx 12
addx -1
addx 3
addx 1
noop
noop
noop
addx -9
addx 18
addx 1
addx 2
noop
noop
addx 9
noop
noop
noop
addx -1
addx 2
addx -37
addx 1
addx 3
noop
addx 15
addx -21
addx 22
addx -6
addx 1
noop
addx 2
addx 1
noop
addx -10
noop
noop
addx 20
addx 1
addx 2
addx 2
addx -6
addx -11
noop
noop
noop

View file

@ -0,0 +1,83 @@
import gleam/io
import gleam/int
import gleam/list
import gleam/string
import gleam/option.{None, Option, Some}
import gleam/erlang/process
pub type Trace {
Trace(x: Int, cycles: Int)
}
pub fn pt_1(input: String) -> Int {
let want = [20, 60, 100, 140, 180, 220]
string.split(input, "\n")
|> list.fold([Trace(1, 0)], execute)
|> list.reverse
|> list.window_by_2
|> collect(want, [])
|> list.reverse
|> list.zip(want)
|> list.map(signal_strength)
|> int.sum
}
pub fn pt_2(input: String) -> Int {
2
}
pub fn execute(trace: List(Trace), instruction: String) -> List(Trace) {
assert [head, ..] = trace
case instruction {
"noop" -> [Trace(head.x, head.cycles + 1), ..trace]
"addx " <> amount -> [
Trace(head.x + parse_int(amount), head.cycles + 2),
..trace
]
}
}
fn parse_int(text: String) -> Int {
assert Ok(i) = int.parse(text)
i
}
fn collect(
trace: List(#(Trace, Trace)),
want: List(Int),
out: List(Int),
) -> List(Int) {
case want {
[] -> out
[n, ..rest] ->
case trace {
[] -> {
io.debug(out)
abort("ran out of traces, want " <> int.to_string(n))
[]
}
[#(a, b), ..traces] ->
case matches(n, a, b) {
Some(x) -> collect(traces, rest, [x, ..out])
None -> collect(traces, want, out)
}
}
}
}
fn abort(msg: String) -> Nil {
io.println(msg)
process.kill(process.self())
}
fn matches(want: Int, a: Trace, b: Trace) -> Option(Int) {
case want >= a.cycles && want <= b.cycles {
True -> Some(a.x)
False -> None
}
}
fn signal_strength(pair: #(Int, Int)) -> Int {
pair.0 * pair.1
}

View file

@ -0,0 +1,25 @@
import gleeunit
import gleeunit/should
import days/day_10.{Trace}
import gleam/erlang/file
import gleam/string
import gleam/list
pub fn main() {
gleeunit.main()
}
pub fn execute_test() {
["noop", "addx 3", "addx -5"]
|> list.fold([Trace(1, 0)], day_10.execute)
|> list.reverse
|> should.equal([Trace(1, 0), Trace(1, 1), Trace(4, 3), Trace(-1, 5)])
}
pub fn pt1_test() {
assert Ok(input) = file.read("input/day_10_pt1_test.txt")
input
|> string.trim_right
|> day_10.pt_1
|> should.equal(13140)
}