Skip to content

Commit

Permalink
drakeerv day 13
Browse files Browse the repository at this point in the history
  • Loading branch information
drakeerv committed Dec 13, 2024
1 parent 6ea8cae commit 62f3fac
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
109 changes: 109 additions & 0 deletions 2024/13/drakeerv.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import os
import arrays

struct Point {
pub mut:
x i64 @[required]
y i64 @[required]
}

fn (p Point) equals(other Point) bool {
return p.x == other.x && p.y == other.y
}

struct Machine {
a Point
b Point
t Point
}

fn parse_button(button string) Point {
split := button.split(', ')
x := i64(split[0].split('+')[1].int())
y := i64(split[1].split('+')[1].int())
return Point{
x: x
y: y
}
}

fn parse_target(target string) Point {
split := target.split(', ')
x := i64(split[0].split('=')[1].int())
y := i64(split[1].split('=')[1].int())
return Point{
x: x
y: y
}
}

fn determinant(a i64, b i64, c i64, d i64) i64 {
return a * d - b * c
}

fn cramers_rule(a i64, b i64, c i64, d i64, e i64, f i64) ?[2]i64 {
det := determinant(a, b, c, d)

if det == 0 {
return none
}

det_x := determinant(e, b, f, d)
det_y := determinant(a, e, c, f)
return [det_x / det, det_y / det]!
}

fn solve(machine Machine) ?[2]i64 {
result := cramers_rule(machine.a.x, machine.b.x, machine.a.y, machine.b.y, machine.t.x,
machine.t.y)

if result != none {
result_point := Point{
x: result[0] * machine.a.x + result[1] * machine.b.x
y: result[0] * machine.a.y + result[1] * machine.b.y
}
if result_point.equals(machine.t) {
return [result[0], result[1]]!
}
}
return none
}

fn main() {
machines := arrays.chunk(os.read_file('machines.input')!.split_into_lines().filter(it != ''),
3).map(fn (machine []string) Machine {
return Machine{
a: parse_button(machine[0])
b: parse_button(machine[1])
t: parse_target(machine[2])
}
})

mut tokens1 := i64(0)
for machine in machines {
result := solve(machine)
if result != none {
tokens1 += ((result[0] * 3) + result[1])
}
}
println('part1: ${tokens1}')

mut tokens2 := i64(0)
for machine in machines {
new_target := Point{
x: machine.t.x + 10000000000000
y: machine.t.y + 10000000000000
}
new_machine := Machine{
a: machine.a
b: machine.b
t: new_target
}

result := solve(new_machine)
if result != none {
tokens2 += ((result[0] * 3) + result[1])
}
}
println('part2: ${tokens2}')
}
15 changes: 15 additions & 0 deletions 2024/13/machines.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Button A: X+94, Y+34
Button B: X+22, Y+67
Prize: X=8400, Y=5400

Button A: X+26, Y+66
Button B: X+67, Y+21
Prize: X=12748, Y=12176

Button A: X+17, Y+86
Button B: X+84, Y+37
Prize: X=7870, Y=6450

Button A: X+69, Y+23
Button B: X+27, Y+71
Prize: X=18641, Y=10279
2 changes: 2 additions & 0 deletions known/2024/13/drakeerv.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
part1: 480
part2: 875318608908

0 comments on commit 62f3fac

Please sign in to comment.