advent-of-code-2023/01/part2.v

51 lines
1.1 KiB
Coq
Raw Normal View History

2023-12-01 19:32:06 +01:00
import os
2023-12-01 23:42:44 +01:00
const input = os.read_file('./input.txt')!
2023-12-01 19:32:06 +01:00
2023-12-01 23:42:44 +01:00
//const input = "two1nine
//eightwothree
//abcone2threexyz
//xtwone3four
//4nineeightseven2
//zoneight234
//7pqrstsixteen"
fn replace_letter_numbers(txt string) string {
mut tmp := txt
tmp = tmp.replace('one', 'o1e')
tmp = tmp.replace('two', 't2o')
tmp = tmp.replace('three', 't3e')
tmp = tmp.replace('four', 'f4r')
tmp = tmp.replace('five', 'f5e')
tmp = tmp.replace('six', 's6x')
tmp = tmp.replace('seven', 's7n')
tmp = tmp.replace('eight', 'e8t')
tmp = tmp.replace('nine', 'n9e')
return tmp
}
2023-12-01 19:32:06 +01:00
fn main() {
lines := input.split('\n')
mut sum := 0
for line in lines {
if line.is_blank() {
continue
}
2023-12-01 23:42:44 +01:00
//println('-> line: ${line}')
replaced := replace_letter_numbers(line)
//println('-> replaced: ${replaced}')
trimmed := replaced.trim('abcdefghijklmnopqrstuvwxyz')
//println('-> trimmed: ${trimmed}')
2023-12-01 19:32:06 +01:00
n := trimmed[0].ascii_str() + trimmed[trimmed.len-1].ascii_str()
2023-12-01 23:42:44 +01:00
//println('=> n: ${n}')
2023-12-01 19:32:06 +01:00
sum += n.int()
}
println("Result is ${sum}")
}