Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2023/01: leodev #17

Merged
merged 2 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions 2023/1/calibration-part1.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
7 changes: 7 additions & 0 deletions 2023/1/calibration-part2.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
58 changes: 58 additions & 0 deletions 2023/1/leodev.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import os { read_file }

fn main() {
inputs_part1 := read_file('calibration-part1.input')!.split_into_lines()
println(part1(inputs_part1))
inputs_part2 := read_file('calibration-part2.input')!.split_into_lines()
println(part2(inputs_part2))
}

fn part1(inputs []string) int {
mut sum := 0
for line in inputs {
mut first := 0
mut last := 0
for c in line {
if c > `0` && c <= `9` {
if first == 0 {
first = c - u8(`0`)
}
last = c - u8(`0`)
}
}
sum += first * 10 + last
}
return sum
}

fn part2(inputs []string) int {
mut sum := 0
replacers := ['1', 'one', '2', 'two', '3', 'three', '4', 'four', '5', 'five', '6', 'six', '7',
'seven', '8', 'eight', '9', 'nine']
values := ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
for old_line in inputs {
line := old_line.replace_each(replacers)
reversed := line.reverse()

mut first := 0
mut first_index := 999
mut last := 0
mut last_index := 999
for idx, value in values {
if front_index := line.index(value) {
if front_index < first_index {
first = idx + 1
first_index = front_index
}
}
if back_index := reversed.index(value.reverse()) {
if back_index < last_index {
last = idx + 1
last_index = back_index
}
}
}
sum += first * 10 + last
}
return sum
}
2 changes: 2 additions & 0 deletions known_outputs/2023/1/leodev.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
142
281
Loading