finish day 6
This commit is contained in:
@@ -14,17 +14,26 @@ sample :: `123 328 51 64
|
|||||||
* + * + `
|
* + * + `
|
||||||
|
|
||||||
Operation :: struct($N: uint) {
|
Operation :: struct($N: uint) {
|
||||||
values: [N]u64,
|
values: [N]i64,
|
||||||
operation: rune,
|
operation: rune,
|
||||||
}
|
}
|
||||||
|
|
||||||
evaluate_op :: proc(op: Operation($N)) -> u64 {
|
new_op :: proc($N: uint) -> Operation(N) {
|
||||||
op := op
|
return {values = {-1, -1, -1}}
|
||||||
if op.operation == '+' {
|
|
||||||
return math.sum(op.values[:])
|
|
||||||
} else {
|
|
||||||
return math.prod(op.values[:])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
evaluate_op :: proc(op: Operation($N)) -> u64 {
|
||||||
|
total: u64
|
||||||
|
for n in op.values {
|
||||||
|
if n == -1 || n == 0 do continue
|
||||||
|
if op.operation == '+' {
|
||||||
|
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) {
|
||||||
@@ -50,7 +59,7 @@ parse_operations :: proc(s: string, $N: uint) -> [dynamic]Operation(N) {
|
|||||||
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 {
|
||||||
@@ -83,5 +92,60 @@ solve_first_sample :: proc(t: ^testing.T) {
|
|||||||
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user