-
Notifications
You must be signed in to change notification settings - Fork 1
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
5 changed files
with
27 additions
and
27 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
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 |
---|---|---|
|
@@ -22,16 +22,16 @@ Maintainer : [email protected] | |
20 | ||
-} | ||
module Main where | ||
module Main (main) where | ||
|
||
import Data.Array.Unboxed (Ix(rangeSize), UArray, accumArray) | ||
import Data.Array.Unboxed (UArray, assocs, rangeSize, UArray, accumArray) | ||
import Data.List (foldl', tails) | ||
import Data.Map qualified as Map | ||
import Data.Maybe (fromMaybe) | ||
import Data.Set (Set) | ||
import Data.Set qualified as Set | ||
|
||
import Advent (arrIx, getInputMap) | ||
import Advent (arrIx, getInputArray) | ||
import Advent.Coord (Coord, above, below, boundingBox, left, neighbors, right) | ||
|
||
-- | | ||
|
@@ -40,7 +40,8 @@ import Advent.Coord (Coord, above, below, boundingBox, left, neighbors, right) | |
-- 1023 | ||
main :: IO () | ||
main = | ||
do elves <- Map.keysSet . Map.filter ('#'==) <$> getInputMap 2022 23 | ||
do input <- getInputArray 2022 23 | ||
let elves = Set.fromList [c | (c, '#') <- assocs input] | ||
let states = sim elves | ||
|
||
-- part 1 | ||
|
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 |
---|---|---|
|
@@ -27,12 +27,12 @@ Maintainer : [email protected] | |
-} | ||
module Main (main) where | ||
|
||
import Data.Map (Map) | ||
import Data.Map qualified as Map | ||
import Data.Char (isDigit) | ||
|
||
import Advent (getInputMap, ordNub) | ||
import Advent (getInputArray, ordNub, arrIx) | ||
import Advent.Coord (Coord, left, neighbors, right) | ||
import Data.Array.Unboxed (UArray, assocs) | ||
import Data.Char (isDigit) | ||
import Data.Map qualified as Map | ||
import Data.Maybe (fromMaybe) | ||
|
||
-- | Parse the input schematic and print answers to both parts. | ||
-- | ||
|
@@ -41,21 +41,21 @@ import Advent.Coord (Coord, left, neighbors, right) | |
-- 81463996 | ||
main :: IO () | ||
main = | ||
do input <- getInputMap 2023 3 | ||
do input <- getInputArray 2023 3 | ||
let numbers = extractNumbers input | ||
print (sum [partNo | (partNo, _:_) <- numbers]) | ||
print (sum [a * b | [a, b] <- gearNumbers numbers]) | ||
|
||
-- | Extract the numbers from the diagram and the parts adjacent to them. | ||
extractNumbers :: Map Coord Char -> [(Int, [(Coord, Char)])] | ||
extractNumbers :: UArray Coord Char -> [(Int, [(Coord, Char)])] | ||
extractNumbers input = | ||
[ (read digits, partsNear cs) | ||
| (c, digit) <- Map.assocs input | ||
| (c, digit) <- assocs input | ||
, isDigit digit, not (isDigit (lkp (left c))) -- left-boundary of number | ||
, let (cs, digits) = unzip (numbersAfter c) | ||
] | ||
where | ||
lkp i = Map.findWithDefault '.' i input | ||
lkp = fromMaybe '.' . arrIx input | ||
numbersAfter start = | ||
[ (c, digit) | ||
| c <- iterate right start | ||
|
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