-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75d0f1b
commit feed7f3
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |