Skip to content

Commit

Permalink
day 3 done
Browse files Browse the repository at this point in the history
  • Loading branch information
Javran committed Jan 7, 2024
1 parent 534a4b4 commit f572505
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Solutions, scripting, and templates - all in one repo.

| `<++++++++>` | `<++++++++>` | `<++++++++>` | `<++++++++>` | `<++++++++>` |
| :-: | :-: | :-: | :-: | :-: |
|[Day 1](src/Javran/AdventOfCode/Y2023/Day1.hs) |[Day 2](src/Javran/AdventOfCode/Y2023/Day2.hs) | [Day 3](src/Javran/AdventOfCode/Y2023/Day3.hs) | | |
|[Day 1](src/Javran/AdventOfCode/Y2023/Day1.hs) |[Day 2](src/Javran/AdventOfCode/Y2023/Day2.hs) | [Day 3](src/Javran/AdventOfCode/Y2023/Day3.hs) | | |

### 2022

Expand Down
2 changes: 2 additions & 0 deletions data/testdata/2023/day/3/example.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
4361
467835
60 changes: 47 additions & 13 deletions src/Javran/AdventOfCode/Y2023/Day3.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,56 @@ data Day3 deriving (Generic)
type ASym = Char -- a symbol
type ANum = (Int, Int) -- a number: (value, length)

type Schematic = (M.Map Coord ASym, [(Coord, ANum)])

parseLine :: String -> [(Int, Either ASym ANum)]
parseLine = go . zip [0 ..]
where
go xs0
| null xs0 = []
| (_, '.') : xs1 <- xs0 = go xs1
| (ds, xs1) <- span (isDigit . snd) xs0 =
case ds of
((r0, _) : _) ->
((r0, Right (read (fmap snd ds), length ds)) :: (Int, Either ASym ANum)) : go xs1
[] -> case xs1 of
(r0, hd) : tl | hd /= '.' -> (r0, Left hd) : go tl
_ -> go xs1
go xs0 = case span (isDigit . snd) xs0 of
(ds@((r0, _) : _), xs1) -> (r0, Right (read (fmap snd ds), length ds)) : go xs1
([], []) -> []
([], (_, '.') : xs1) -> go xs1
([], (r0, ch) : xs1) -> (r0, Left ch) : go xs1

parseSchematic :: [String] -> Schematic
parseSchematic inp = (M.fromList syms, nums)
where
tokens :: [(Coord, Either ASym ANum)]
tokens = do
(r, rawLine) <- zip [0 ..] inp
let parsed = parseLine rawLine
(fmap . first) (r,) parsed

(syms, nums) =
partitionEithers $
fmap (\(coord, e) -> bimap (coord,) (coord,) e) tokens

isAdjacent :: (Coord, ANum) -> Coord -> Bool
isAdjacent ((nr, nc), (_, nl)) (sr, sc) =
nr - 1 <= sr && sr <= nr + 1 && nc - 1 <= sc && sc <= nc + nl

instance Solution Day3 where
solutionSolved _ = False
solutionSolved _ = True
solutionRun _ SolutionContext {getInputS, answerShow} = do
xs <- fmap id . lines <$> getInputS
mapM_ (print . parseLine) xs
xs <- lines <$> getInputS
let (syms, nums) = parseSchematic xs
do
let rs = filter (\n -> any (isAdjacent n) $ M.keys syms) nums
answerShow $ sum $ fmap (fst . snd) rs
let mayGearCoords :: [Coord]
mayGearCoords = M.keys $ M.filter (== '*') syms
adjacentCounts :: M.Map Coord [Int]
adjacentCounts = M.fromListWith (<>) do
n@(_, (v, _)) <- nums
g <- mayGearCoords
guard $ isAdjacent n g
pure (g, [v])
answerShow
. getSum
. foldMap
( \case
[a, b] -> Sum $ a * b
_ -> 0
)
$ M.elems
adjacentCounts
2 changes: 1 addition & 1 deletion test/Javran/AdventOfCode/TestdataSpec.hs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f572505

Please sign in to comment.