From 62f3fac9dd3db4a8c7794703ad4fa208e45961d5 Mon Sep 17 00:00:00 2001 From: drakeerv Date: Fri, 13 Dec 2024 13:06:19 -0500 Subject: [PATCH] drakeerv day 13 --- 2024/13/drakeerv.v | 109 +++++++++++++++++++++++++++++++++++++ 2024/13/machines.input | 15 +++++ known/2024/13/drakeerv.out | 2 + 3 files changed, 126 insertions(+) create mode 100644 2024/13/drakeerv.v create mode 100644 2024/13/machines.input create mode 100644 known/2024/13/drakeerv.out diff --git a/2024/13/drakeerv.v b/2024/13/drakeerv.v new file mode 100644 index 0000000..f160c72 --- /dev/null +++ b/2024/13/drakeerv.v @@ -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}') +} diff --git a/2024/13/machines.input b/2024/13/machines.input new file mode 100644 index 0000000..912f482 --- /dev/null +++ b/2024/13/machines.input @@ -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 diff --git a/known/2024/13/drakeerv.out b/known/2024/13/drakeerv.out new file mode 100644 index 0000000..425cbbd --- /dev/null +++ b/known/2024/13/drakeerv.out @@ -0,0 +1,2 @@ +part1: 480 +part2: 875318608908