complete hacky day 6 part 1
This commit is contained in:
87
day6/day6.odin
Normal file
87
day6/day6.odin
Normal file
@@ -0,0 +1,87 @@
|
||||
package day6
|
||||
|
||||
import "core:log"
|
||||
import math "core:math"
|
||||
import "core:strconv"
|
||||
import "core:strings"
|
||||
import "core:testing"
|
||||
|
||||
import "../utils"
|
||||
|
||||
sample :: `123 328 51 64
|
||||
45 64 387 23
|
||||
6 98 215 314
|
||||
* + * + `
|
||||
|
||||
Operation :: struct($N: uint) {
|
||||
values: [N]u64,
|
||||
operation: rune,
|
||||
}
|
||||
|
||||
evaluate_op :: proc(op: Operation($N)) -> u64 {
|
||||
op := op
|
||||
if op.operation == '+' {
|
||||
return math.sum(op.values[:])
|
||||
} else {
|
||||
return math.prod(op.values[:])
|
||||
}
|
||||
}
|
||||
|
||||
parse_operations :: proc(s: string, $N: uint) -> [dynamic]Operation(N) {
|
||||
ops: [dynamic]Operation(N)
|
||||
num_elements := 0
|
||||
counter := s
|
||||
for line in strings.split_lines_iterator(&counter) {
|
||||
line := line
|
||||
for x in strings.split_iterator(&line, " ") {
|
||||
(x != "") or_continue
|
||||
num_elements += 1
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
resize(&ops, num_elements)
|
||||
|
||||
row := 0
|
||||
buf := s
|
||||
for line in strings.split_lines_iterator(&buf) {
|
||||
(line != "") or_continue
|
||||
line := line
|
||||
col := 0
|
||||
for x in strings.split_iterator(&line, " ") {
|
||||
(x != "") or_continue
|
||||
val, ok := strconv.parse_u64(x)
|
||||
if ok {
|
||||
ops[col].values[row] = val
|
||||
} else {
|
||||
ops[col].operation = rune(x[0])
|
||||
}
|
||||
col += 1
|
||||
}
|
||||
row += 1
|
||||
}
|
||||
|
||||
return ops
|
||||
}
|
||||
|
||||
solve_first :: proc(s: string, $N: uint) -> u64 {
|
||||
ops := parse_operations(s, N)
|
||||
defer delete(ops)
|
||||
total: u64 = 0
|
||||
for op in ops {
|
||||
total += evaluate_op(op)
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
@(test)
|
||||
solve_first_sample :: proc(t: ^testing.T) {
|
||||
testing.expect_value(t, solve_first(sample, 3), 4277556)
|
||||
}
|
||||
|
||||
@(test)
|
||||
solve_first_puzzle :: proc(t: ^testing.T) {
|
||||
content := utils.read_file("day6/input.txt")
|
||||
defer delete(content)
|
||||
testing.expect_value(t, solve_first(content, 4), 4277556)
|
||||
}
|
||||
Reference in New Issue
Block a user