Skip to content

Commit

Permalink
Krotki day 5
Browse files Browse the repository at this point in the history
  • Loading branch information
Krotki committed Dec 5, 2024
1 parent feed7f3 commit deadb7c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
28 changes: 28 additions & 0 deletions 2024/05/krotki/part1.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module main

import os

fn main() {
rules := os.get_lines().map(it.split('|').map(it.int()))
updates := os.get_lines().map(it.split(',').map(it.int()))

mut sum := 0
updatefor: for update in updates {
for i, page in update {
selected_rules := rules.filter(it[0] == page)
for rule in selected_rules {
page_index := update.index(rule[1])
if page_index == -1 {
continue
}
if i > page_index {
continue updatefor
}
}
}
// update is valid at this point
sum += update[update.len / 2]
}

println(sum)
}
39 changes: 39 additions & 0 deletions 2024/05/krotki/part2.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module main

import os

fn main() {
rules := os.get_lines().map(it.split('|').map(it.int()))
updates := os.get_lines().map(it.split(',').map(it.int()))

mut sum := 0
updatefor: for update in updates {
for i, page in update {
selected_rules := rules.filter(it[0] == page)
for rule in selected_rules {
page_index := update.index(rule[1])
if page_index == -1 {
continue
}
if i > page_index {
mut prio := map[int]int{}
for n in update {
for r in rules.filter(it[0] == n && update.contains(it[1])) {
prio[r[0]] = (prio[r[0]] or { 0 }) - 1
prio[r[1]] = (prio[r[1]] or { 0 }) + 1
}
}
// dump(prio)
sorted := update.sorted_with_compare(fn [prio] (a &int, b &int) int {
return (prio[*b] or { 0 }) - (prio[*a] or { 0 })
})

sum += sorted[sorted.len / 2]
continue updatefor
}
}
}
}

println(sum)
}
1 change: 1 addition & 0 deletions known/2024/05/krotki/part1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
143
1 change: 1 addition & 0 deletions known/2024/05/krotki/part2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
123

0 comments on commit deadb7c

Please sign in to comment.