Skip to content

Commit

Permalink
leodev, day 05 (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
Le0Developer authored Dec 5, 2024
1 parent 75d0f1b commit feed7f3
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
60 changes: 60 additions & 0 deletions 2024/05/leodev.v
Original file line number Diff line number Diff line change
@@ -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}')
}
28 changes: 28 additions & 0 deletions 2024/05/pages.input
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions known/2024/05/leodev.out
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit feed7f3

Please sign in to comment.