Skip to content

Commit d98021d

Browse files
authored
drakeerv, day 13 (#74)
1 parent 6ea8cae commit d98021d

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

2024/13/drakeerv.v

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import os
2+
import arrays
3+
4+
struct Point {
5+
pub mut:
6+
x i64 @[required]
7+
y i64 @[required]
8+
}
9+
10+
fn (p Point) equals(other Point) bool {
11+
return p.x == other.x && p.y == other.y
12+
}
13+
14+
struct Machine {
15+
a Point
16+
b Point
17+
t Point
18+
}
19+
20+
fn parse_button(button string) Point {
21+
split := button.split(', ')
22+
x := i64(split[0].split('+')[1].int())
23+
y := i64(split[1].split('+')[1].int())
24+
return Point{
25+
x: x
26+
y: y
27+
}
28+
}
29+
30+
fn parse_target(target string) Point {
31+
split := target.split(', ')
32+
x := i64(split[0].split('=')[1].int())
33+
y := i64(split[1].split('=')[1].int())
34+
return Point{
35+
x: x
36+
y: y
37+
}
38+
}
39+
40+
fn determinant(a i64, b i64, c i64, d i64) i64 {
41+
return a * d - b * c
42+
}
43+
44+
fn cramers_rule(a i64, b i64, c i64, d i64, e i64, f i64) ?[2]i64 {
45+
det := determinant(a, b, c, d)
46+
47+
if det == 0 {
48+
return none
49+
}
50+
51+
det_x := determinant(e, b, f, d)
52+
det_y := determinant(a, e, c, f)
53+
return [det_x / det, det_y / det]!
54+
}
55+
56+
fn solve(machine Machine) ?[2]i64 {
57+
result := cramers_rule(machine.a.x, machine.b.x, machine.a.y, machine.b.y, machine.t.x,
58+
machine.t.y)
59+
60+
if result != none {
61+
result_point := Point{
62+
x: result[0] * machine.a.x + result[1] * machine.b.x
63+
y: result[0] * machine.a.y + result[1] * machine.b.y
64+
}
65+
if result_point.equals(machine.t) {
66+
return [result[0], result[1]]!
67+
}
68+
}
69+
return none
70+
}
71+
72+
fn main() {
73+
machines := arrays.chunk(os.read_file('machines.input')!.split_into_lines().filter(it != ''),
74+
3).map(fn (machine []string) Machine {
75+
return Machine{
76+
a: parse_button(machine[0])
77+
b: parse_button(machine[1])
78+
t: parse_target(machine[2])
79+
}
80+
})
81+
82+
mut tokens1 := i64(0)
83+
for machine in machines {
84+
result := solve(machine)
85+
if result != none {
86+
tokens1 += ((result[0] * 3) + result[1])
87+
}
88+
}
89+
println('part1: ${tokens1}')
90+
91+
mut tokens2 := i64(0)
92+
for machine in machines {
93+
new_target := Point{
94+
x: machine.t.x + 10000000000000
95+
y: machine.t.y + 10000000000000
96+
}
97+
new_machine := Machine{
98+
a: machine.a
99+
b: machine.b
100+
t: new_target
101+
}
102+
103+
result := solve(new_machine)
104+
if result != none {
105+
tokens2 += ((result[0] * 3) + result[1])
106+
}
107+
}
108+
println('part2: ${tokens2}')
109+
}

2024/13/machines.input

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Button A: X+94, Y+34
2+
Button B: X+22, Y+67
3+
Prize: X=8400, Y=5400
4+
5+
Button A: X+26, Y+66
6+
Button B: X+67, Y+21
7+
Prize: X=12748, Y=12176
8+
9+
Button A: X+17, Y+86
10+
Button B: X+84, Y+37
11+
Prize: X=7870, Y=6450
12+
13+
Button A: X+69, Y+23
14+
Button B: X+27, Y+71
15+
Prize: X=18641, Y=10279

known/2024/13/drakeerv.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
part1: 480
2+
part2: 875318608908

0 commit comments

Comments
 (0)