day 1, part 2

This commit is contained in:
Bruno Carlin 2023-12-01 23:42:44 +01:00
parent b048d63227
commit 197e08e850
Signed by: bcarlin
GPG key ID: 8E254EA0FFEB9B6D
2 changed files with 35 additions and 9 deletions

View file

@ -1,6 +1,10 @@
import os
const input = os.read_file('./input.txt')!
//const input = "1abc2
//pqr3stu8vwx
//a1b2c3d4e5f
//treb7uchet"
fn main() {
lines := input.split('\n')

View file

@ -1,14 +1,31 @@
import os
//const input = os.read_file('./input.txt')!
const input = os.read_file('./input.txt')!
const input = "two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen"
//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
}
fn main() {
lines := input.split('\n')
@ -19,8 +36,13 @@ fn main() {
continue
}
trimmed := line.trim('abcdefghijklmnopqrstuvwxyz')
//println('-> line: ${line}')
replaced := replace_letter_numbers(line)
//println('-> replaced: ${replaced}')
trimmed := replaced.trim('abcdefghijklmnopqrstuvwxyz')
//println('-> trimmed: ${trimmed}')
n := trimmed[0].ascii_str() + trimmed[trimmed.len-1].ascii_str()
//println('=> n: ${n}')
sum += n.int()
}