diff --git a/2024/05/leodev.v b/2024/05/leodev.v new file mode 100644 index 0000000..2638d69 --- /dev/null +++ b/2024/05/leodev.v @@ -0,0 +1,60 @@ +import os + +fn main() { + lines := os.read_lines('pages.input')! + + mut rules := [][]int{len: 100} + mut rules2 := [][]int{len: 100} + mut offset := 0 + for line in lines { + offset++ + if line == '' { + break + } + + a, b := line.split_once('|') or { panic(err) } + rules[a.int()] << b.int() + rules2[b.int()] << a.int() + } + + mut total1 := 0 + mut total2 := 0 + for line in lines[offset..] { + pages := line.split(',').map(it.int()) + mut valid := true + outer: for i, page in pages { + for value in rules[page] { + idx := pages.index(value) + if idx < i && idx != -1 { + println('${value} at ${idx} must be before ${page} at ${i}') + valid = false + break outer + } + } + } + if valid { + println('valid: ${line}') + total1 += pages[pages.len / 2] + } else { + mut correctly_ordered := []int{} + mut remaining_pages := pages.clone() + for remaining_pages.len > 0 { + outer2: for i, potential in remaining_pages { + for conflict in remaining_pages[1..] { + if rules2[potential].contains(conflict) { + continue outer2 + } + } + correctly_ordered << potential + remaining_pages.delete(i) + break + } + } + println('corrected: ${correctly_ordered}') + total2 += correctly_ordered[correctly_ordered.len / 2] + } + } + + println('part1: ${total1}') + println('part2: ${total2}') +} diff --git a/2024/05/pages.input b/2024/05/pages.input new file mode 100644 index 0000000..9d146d6 --- /dev/null +++ b/2024/05/pages.input @@ -0,0 +1,28 @@ +47|53 +97|13 +97|61 +97|47 +75|29 +61|13 +75|53 +29|13 +97|29 +53|29 +61|53 +97|53 +61|29 +47|13 +75|47 +97|75 +47|61 +75|61 +47|29 +75|13 +53|13 + +75,47,61,53,29 +97,61,53,29,13 +75,29,13 +75,97,47,61,53 +61,13,29 +97,13,75,29,47 diff --git a/known/2024/05/leodev.out b/known/2024/05/leodev.out new file mode 100644 index 0000000..c5913ad --- /dev/null +++ b/known/2024/05/leodev.out @@ -0,0 +1,11 @@ +valid: 75,47,61,53,29 +valid: 97,61,53,29,13 +valid: 75,29,13 +75 at 0 must be before 97 at 1 +corrected: [97, 75, 47, 61, 53] +13 at 1 must be before 29 at 2 +corrected: [61, 29, 13] +13 at 1 must be before 75 at 2 +corrected: [97, 75, 47, 29, 13] +part1: 143 +part2: 123