day 5 puzzle 1
This commit is contained in:
parent
590ba743a1
commit
4fb226448f
BIN
progress.png
BIN
progress.png
Binary file not shown.
Before Width: | Height: | Size: 110 KiB After Width: | Height: | Size: 111 KiB |
@ -5,4 +5,6 @@
|
||||
3:
|
||||
stars: 2
|
||||
4:
|
||||
stars: 2
|
||||
stars: 2
|
||||
5:
|
||||
stars: 1
|
28
res/examples/day5.txt
Normal file
28
res/examples/day5.txt
Normal file
@ -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
|
52
src/day5/puzzle1.typ
Normal file
52
src/day5/puzzle1.typ
Normal file
@ -0,0 +1,52 @@
|
||||
#import "/src/utils.typ": *
|
||||
|
||||
#let make-rules-dict(rules) = {
|
||||
let dict = (:)
|
||||
|
||||
for rule in rules {
|
||||
let (a, b) = rule
|
||||
|
||||
if a not in dict {
|
||||
dict.insert(a, ())
|
||||
}
|
||||
dict.at(a).push(b)
|
||||
}
|
||||
|
||||
return dict
|
||||
}
|
||||
|
||||
#let is-update-valid(dict, update) = {
|
||||
for i in range(update.len() - 1) {
|
||||
let a = str(update.at(i))
|
||||
for j in range(i + 1, update.len()) {
|
||||
let b = str(update.at(j))
|
||||
if a in dict.at(b, default: ()) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
#let solve(input) = {
|
||||
let (rules, updates) = input.split("\n\n")
|
||||
rules = rules.split("\n").map(l => l.split("|"))
|
||||
updates = updates.split("\n").map(l => l.split(",").map(int))
|
||||
|
||||
let total = 0
|
||||
let rules-dict = make-rules-dict(rules)
|
||||
|
||||
for update in updates {
|
||||
if is-update-valid(rules-dict, update) {
|
||||
total += update.at(calc.div-euclid(update.len(), 2))
|
||||
}
|
||||
}
|
||||
|
||||
return total
|
||||
}
|
||||
|
||||
#show-puzzle(
|
||||
5, 1,
|
||||
solve,
|
||||
example: 143
|
||||
)
|
0
src/day5/puzzle2.typ
Normal file
0
src/day5/puzzle2.typ
Normal file
BIN
src/main.pdf
BIN
src/main.pdf
Binary file not shown.
BIN
src/utils.pdf
Normal file
BIN
src/utils.pdf
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user