Skip to content

Commit

Permalink
leodev, day 07 (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
Le0Developer authored Dec 7, 2024
1 parent 7c4c87b commit 17f1251
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
Empty file removed 2024/07/.gitkeep
Empty file.
42 changes: 42 additions & 0 deletions 2024/07/leodev.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import os

fn main() {
lines := os.read_lines('calibration.input')!

mut total1 := i64(0)
mut total2 := i64(0)
for line in lines {
expected_str, value_str := line.split_once(': ') or { panic(err) }
expected := expected_str.i64()
values := value_str.split(' ').map(it.i64())

if find(expected, 0, values, false) {
total1 += expected
}
if find(expected, 0, values, true) {
total2 += expected
}
}

println('part1: ${total1}')
println('part2: ${total2}')
}

fn find(expected i64, current_value i64, remaining_values []i64, part2 bool) bool {
if remaining_values.len == 0 {
return expected == current_value
}
if current_value > expected {
return false
}

if find(expected, current_value + remaining_values[0], remaining_values[1..], part2) {
return true
}
if part2
&& find(expected, (current_value.str() + remaining_values[0].str()).i64(), remaining_values[1..], part2) {
return true
}
return find(expected, current_value * remaining_values[0], remaining_values[1..],
part2)
}
Empty file removed known/2024/07/.gitkeep
Empty file.
2 changes: 2 additions & 0 deletions known/2024/07/leodev.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
part1: 3749
part2: 11387

0 comments on commit 17f1251

Please sign in to comment.