-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c4c87b
commit 17f1251
Showing
4 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
part1: 3749 | ||
part2: 11387 |