|
| 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 | +} |
0 commit comments