-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
8 changed files
with
1,515 additions
and
13 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 |
---|---|---|
@@ -1,2 +1,29 @@ | ||
def exampleInput := | ||
"..." | ||
"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
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
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 |
---|---|---|
@@ -1,15 +1,38 @@ | ||
import Aoc2024.Utils | ||
import Aoc2024.Day05.Examples | ||
import Aoc2024.Day05.Parser | ||
open Std (HashSet) | ||
|
||
private def solvePart1 (things : List Int) : Int := sorry | ||
-- Part 1 | ||
|
||
def parseAndSolvePart1 (s : String): Except String Int := parseThings s |>.map solvePart1 | ||
private def middleElement {α} (l : List α) : Option α := l.get? (l.length / 2) | ||
|
||
-- #guard parseAndSolvePart1 exampleInput == Except.ok 2 | ||
private def middlePageNumber (update : Update) : Int := middleElement update.pages |>.getD 0 | ||
|
||
private def solvePart2 (things : List Int) : Int := sorry | ||
private def isCorrectlyOrdered (rules : List OrderingRule) (update : Update) : Bool := | ||
let allowedPairs := rules.map (·.toPair) |>.toSet | ||
update.pages.slidingPairs.toSet.isSubsetOf allowedPairs | ||
|
||
def parseAndSolvePart2 (s : String): Except String Int := parseThings s |>.map solvePart2 | ||
private def solvePart1 (input : PuzzleInput) : Int := | ||
input.updates.filter (isCorrectlyOrdered input.rules) | ||
|>.sumBy middlePageNumber | ||
|
||
-- #guard parseAndSolvePart2 exampleInput == Except.ok 4 | ||
def parseAndSolvePart1 (s : String): Except String Int := parseInput s |>.map solvePart1 | ||
|
||
#guard parseAndSolvePart1 exampleInput == Except.ok 143 | ||
|
||
-- Part 2 | ||
|
||
private def orderCorrectly (rules : List OrderingRule) (update : Update) : Update := | ||
let allowedPairs := rules.map (·.toPair) |>.toSet | ||
let reorderedPages := update.pages.mergeSort (λ p1 p2 => p1 == p2 || allowedPairs.contains (p1, p2)) | ||
{ pages := reorderedPages } | ||
|
||
private def solvePart2 (input : PuzzleInput) : Int := | ||
input.updates.filterNot (isCorrectlyOrdered input.rules) | ||
|>.map (orderCorrectly input.rules) | ||
|>.sumBy middlePageNumber | ||
|
||
def parseAndSolvePart2 (s : String): Except String Int := parseInput s |>.map solvePart2 | ||
|
||
#guard parseAndSolvePart2 exampleInput == Except.ok 123 |
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,18 @@ | ||
abbrev Page := Int | ||
|
||
structure OrderingRule where | ||
before : Page | ||
after : Page | ||
deriving BEq, Hashable, Repr | ||
|
||
def OrderingRule.toPair (r : OrderingRule) : Page × Page := | ||
(r.before, r.after) | ||
|
||
structure Update where | ||
pages : List Page | ||
deriving BEq, Hashable, Repr | ||
|
||
structure PuzzleInput where | ||
rules : List OrderingRule | ||
updates : List Update | ||
deriving BEq, Hashable, Repr |
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 |
Oops, something went wrong.