solve AOC day 4 pt 2
This commit is contained in:
@@ -4,11 +4,12 @@ import haxe.ds.Option;
|
|||||||
|
|
||||||
typedef Board = {
|
typedef Board = {
|
||||||
uncalled:Array<Array<Option<Int>>>,
|
uncalled:Array<Array<Option<Int>>>,
|
||||||
called:Array<Array<Option<Int>>>
|
called:Array<Array<Option<Int>>>,
|
||||||
|
won:Bool
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef GameState = {
|
typedef GameState = {
|
||||||
numbersToCall:Array<Int>,
|
numbersToCall:Array<Int>,
|
||||||
boards:Array<Board>,
|
boards:Array<Board>,
|
||||||
boardsByNumber:Map<Int,Array<Board>>
|
boardsByNumber:Map<Int,Array<Board>>,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -17,7 +17,9 @@
|
|||||||
(day 4
|
(day 4
|
||||||
(load "day4.kiss")
|
(load "day4.kiss")
|
||||||
(assert (= 4512 (winningScore "src/year2021/inputs/day4-example.txt")))
|
(assert (= 4512 (winningScore "src/year2021/inputs/day4-example.txt")))
|
||||||
(assert (= 46920 (winningScore "src/year2021/inputs/day4.txt"))))
|
(assert (= 46920 (winningScore "src/year2021/inputs/day4.txt")))
|
||||||
|
(assert (= 1924 (lastWinningScore "src/year2021/inputs/day4-example.txt")))
|
||||||
|
(assert (= 12635 (lastWinningScore "src/year2021/inputs/day4.txt"))))
|
||||||
(dayTodo 5)
|
(dayTodo 5)
|
||||||
(day 6
|
(day 6
|
||||||
(load "day6.kiss")
|
(load "day6.kiss")
|
||||||
|
|||||||
@@ -8,7 +8,8 @@
|
|||||||
(let [numbers (map (apply concat (for line group (filter (line.split " ")))) Std.parseInt)
|
(let [numbers (map (apply concat (for line group (filter (line.split " ")))) Std.parseInt)
|
||||||
board (object
|
board (object
|
||||||
uncalled (for line (groups numbers 5) (for number line (Some number)))
|
uncalled (for line (groups numbers 5) (for number line (Some number)))
|
||||||
called (for y (range 5) (for x (range 5) None)))]
|
called (for y (range 5) (for x (range 5) None))
|
||||||
|
won false)]
|
||||||
(doFor number numbers
|
(doFor number numbers
|
||||||
(unless (boardsByNumber.exists number) (dictSet boardsByNumber number []))
|
(unless (boardsByNumber.exists number) (dictSet boardsByNumber number []))
|
||||||
(.push (dictGet boardsByNumber number) board))
|
(.push (dictGet boardsByNumber number) board))
|
||||||
@@ -39,14 +40,18 @@
|
|||||||
(when won (return true))))
|
(when won (return true))))
|
||||||
(return false)))
|
(return false)))
|
||||||
|
|
||||||
// Return the winning score when a board wins
|
// Return the winning score(s) when a board(s) win
|
||||||
(function :Null<Int> stepState [:GameState state]
|
(function :Null<Array<Int>> stepState [:GameState state]
|
||||||
(let [numberCalled (state.numbersToCall.shift)]
|
// Keep returning the last score if there are no more numbers to call
|
||||||
|
(let [numberCalled (state.numbersToCall.shift)
|
||||||
|
winningScores []]
|
||||||
(when (state.boardsByNumber.exists numberCalled)
|
(when (state.boardsByNumber.exists numberCalled)
|
||||||
(doFor board (dictGet state.boardsByNumber numberCalled)
|
(doFor board (dictGet state.boardsByNumber numberCalled)
|
||||||
(when (stepBoard board numberCalled)
|
(unless board.won
|
||||||
(return (boardScore board numberCalled)))))
|
(when (stepBoard board numberCalled)
|
||||||
null))
|
(set board.won true)
|
||||||
|
(winningScores.push (boardScore board numberCalled))))))
|
||||||
|
winningScores))
|
||||||
|
|
||||||
(function :Int boardScore [:Board board numberCalled]
|
(function :Int boardScore [:Board board numberCalled]
|
||||||
(* numberCalled (apply + (for row board.uncalled (apply + (for square row (case square ((Some v) v) (otherwise 0))))))))
|
(* numberCalled (apply + (for row board.uncalled (apply + (for square row (case square ((Some v) v) (otherwise 0))))))))
|
||||||
@@ -54,6 +59,17 @@
|
|||||||
(function winningScore [file]
|
(function winningScore [file]
|
||||||
(let [state (readState file)]
|
(let [state (readState file)]
|
||||||
(loop
|
(loop
|
||||||
(whenLet [winningScore (stepState state)]
|
// Assume that for potential solutions to the AOC problem, only one board wins at a time.
|
||||||
(return winningScore))))
|
(whenLet [[score] (stepState state)]
|
||||||
|
(return score))))
|
||||||
|
(throw ""))
|
||||||
|
|
||||||
|
(function lastWinningScore [file]
|
||||||
|
(let [state (readState file)
|
||||||
|
&mut finishedBoards 0]
|
||||||
|
(loop
|
||||||
|
(whenLet [winningScores (stepState state)]
|
||||||
|
(+= finishedBoards winningScores.length)
|
||||||
|
(when (= finishedBoards state.boards.length)
|
||||||
|
(return (first winningScores))))))
|
||||||
(throw ""))
|
(throw ""))
|
||||||
@@ -24,7 +24,7 @@
|
|||||||
(dictSet newMap -1 (dictGet theMap -1))
|
(dictSet newMap -1 (dictGet theMap -1))
|
||||||
(doFor age (range 9)
|
(doFor age (range 9)
|
||||||
(let [count (dictGet theMap age)]
|
(let [count (dictGet theMap age)]
|
||||||
(case ~age
|
(case age
|
||||||
// Lanternfish giving birth:
|
// Lanternfish giving birth:
|
||||||
(0
|
(0
|
||||||
(dictInc newMap 8 count)
|
(dictInc newMap 8 count)
|
||||||
|
|||||||
Reference in New Issue
Block a user