finish day 6

This commit is contained in:
2025-12-06 21:41:54 -05:00
parent 01c537f44d
commit d7277a9784
2 changed files with 83 additions and 19 deletions

View File

@@ -9,22 +9,31 @@ import "core:testing"
import "../utils" import "../utils"
sample :: `123 328 51 64 sample :: `123 328 51 64
45 64 387 23 45 64 387 23
6 98 215 314 6 98 215 314
* + * + ` * + * + `
Operation :: struct($N: uint) { Operation :: struct($N: uint) {
values: [N]u64, values: [N]i64,
operation: rune, operation: rune,
} }
new_op :: proc($N: uint) -> Operation(N) {
return {values = {-1, -1, -1}}
}
evaluate_op :: proc(op: Operation($N)) -> u64 { evaluate_op :: proc(op: Operation($N)) -> u64 {
op := op total: u64
if op.operation == '+' { for n in op.values {
return math.sum(op.values[:]) if n == -1 || n == 0 do continue
} else { if op.operation == '+' {
return math.prod(op.values[:]) total += u64(n)
} else {
if total == 0 do total += 1
total *= u64(n)
}
} }
return total
} }
parse_operations :: proc(s: string, $N: uint) -> [dynamic]Operation(N) { parse_operations :: proc(s: string, $N: uint) -> [dynamic]Operation(N) {
@@ -43,14 +52,14 @@ parse_operations :: proc(s: string, $N: uint) -> [dynamic]Operation(N) {
resize(&ops, num_elements) resize(&ops, num_elements)
row := 0 row := 0
buf := s buf := s
for line in strings.split_lines_iterator(&buf) { for line in strings.split_lines_iterator(&buf) {
(line != "") or_continue (line != "") or_continue
line := line line := line
col := 0 col := 0
for x in strings.split_iterator(&line, " ") { for x in strings.split_iterator(&line, " ") {
(x != "") or_continue (x != "") or_continue
val, ok := strconv.parse_u64(x) val, ok := strconv.parse_i64(x)
if ok { if ok {
ops[col].values[row] = val ops[col].values[row] = val
} else { } else {
@@ -71,17 +80,72 @@ solve_first :: proc(s: string, $N: uint) -> u64 {
for op in ops { for op in ops {
total += evaluate_op(op) total += evaluate_op(op)
} }
return total return total
} }
@(test) @(test)
solve_first_sample :: proc(t: ^testing.T) { solve_first_sample :: proc(t: ^testing.T) {
testing.expect_value(t, solve_first(sample, 3), 4277556) testing.expect_value(t, solve_first(sample, 3), 4277556)
} }
@(test) @(test)
solve_first_puzzle :: proc(t: ^testing.T) { solve_first_puzzle :: proc(t: ^testing.T) {
content := utils.read_file("day6/input.txt") content := utils.read_file("day6/input.txt")
defer delete(content) defer delete(content)
testing.expect_value(t, solve_first(content, 4), 4277556) testing.expect_value(t, solve_first(content, 4), 6209956042374)
} }
solve_second :: proc(s: string, $N: uint) -> u64 {
lines := strings.split_lines(s)
defer delete(lines)
assert(N == len(lines) - 1)
ops: [dynamic]Operation(N)
defer delete(ops)
op: Operation(N)
op_i := N - 1
for i in 0 ..< len(lines[0]) {
val: i64
if lines[0][i] != ' ' {
val += i64(lines[0][i] - '0')
}
for j in 1 ..< N {
c := lines[j][i]
if c != ' ' {
val *= 10
val += i64(c - '0')
}
}
if lines[N][i] != ' ' {
op.operation = rune(lines[N][i])
}
if val == 0 {
append(&ops, op)
op = {}
op_i = N - 1
} else {
op.values[op_i] = val
op_i -= 1
}
}
append(&ops, op)
total: u64 = 0
for op in ops {
total += evaluate_op(op)
}
return total
}
@(test)
solve_second_sample :: proc(t: ^testing.T) {
testing.expect_value(t, solve_second(sample, 3), 3263827)
}
@(test)
solve_second_puzzle :: proc(t: ^testing.T) {
content := utils.read_file("day6/input.txt")
defer delete(content)
testing.expect_value(t, solve_second(content, 4), 12608160008022)
}