Skip to content
This repository was archived by the owner on Nov 17, 2024. It is now read-only.

Commit cfb1c85

Browse files
committed
initial day 9
1 parent d6b4f05 commit cfb1c85

File tree

6 files changed

+115
-9
lines changed

6 files changed

+115
-9
lines changed

β€ŽREADME.mdβ€Ž

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ haddocks][haddock] --- more info below!
4242
| Day 2 | [x][d02r] | [x][d02g] | [x][d02h] | [x][d02b] |
4343
| Day 3 | | [x][d03g] | [x][d03h] | [x][d03b] |
4444
| Day 4 | | [x][d04g] | [x][d04h] | [x][d04b] |
45-
| Day 5 | | | | |
45+
| Day 5 | | [x][d05g] | [x][d05h] | [x][d05b] |
4646
| Day 6 | | [x][d06g] | [x][d06h] | [x][d06b] |
4747
| Day 7 | | [x][d07g] | [x][d07h] | [x][d07b] |
4848
| Day 8 | | [x][d08g] | [x][d08h] | [x][d08b] |
@@ -309,6 +309,9 @@ All networking features are powered by *[advent-of-code-api][]*.
309309
[d04g]: https://github.com/mstksg/advent-of-code-2021/blob/master/src/AOC/Challenge/Day04.hs
310310
[d04h]: https://mstksg.github.io/advent-of-code-2021/src/AOC.Challenge.Day04.html
311311
[d04b]: https://github.com/mstksg/advent-of-code-2021/blob/master/reflections.md#day-4-benchmarks
312+
[d05g]: https://github.com/mstksg/advent-of-code-2021/blob/master/src/AOC/Challenge/Day05.hs
313+
[d05h]: https://mstksg.github.io/advent-of-code-2021/src/AOC.Challenge.Day05.html
314+
[d05b]: https://github.com/mstksg/advent-of-code-2021/blob/master/reflections.md#day-5-benchmarks
312315
[d06g]: https://github.com/mstksg/advent-of-code-2021/blob/master/src/AOC/Challenge/Day06.hs
313316
[d06h]: https://mstksg.github.io/advent-of-code-2021/src/AOC.Challenge.Day06.html
314317
[d06b]: https://github.com/mstksg/advent-of-code-2021/blob/master/reflections.md#day-6-benchmarks

β€Žreflections.mdβ€Ž

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Table of Contents
2525
* [Day 2](#day-2)
2626
* [Day 3](#day-3) *(no reflection yet)*
2727
* [Day 4](#day-4) *(no reflection yet)*
28+
* [Day 5](#day-5) *(no reflection yet)*
2829
* [Day 6](#day-6) *(no reflection yet)*
2930
* [Day 7](#day-7) *(no reflection yet)*
3031
* [Day 8](#day-8) *(no reflection yet)*
@@ -389,6 +390,50 @@ variance introduced by outliers: 59% (severely inflated)
389390

390391

391392

393+
Day 5
394+
------
395+
396+
<!--
397+
This section is generated and compiled by the build script at ./Build.hs from
398+
the file `./reflections/day05.md`. If you want to edit this, edit
399+
that file instead!
400+
-->
401+
402+
*[Prompt][d05p]* / *[Code][d05g]* / *[Rendered][d05h]* / *[Standalone Reflection Page][d05r]*
403+
404+
[d05p]: https://adventofcode.com/2021/day/5
405+
[d05g]: https://github.com/mstksg/advent-of-code-2021/blob/master/src/AOC/Challenge/Day05.hs
406+
[d05h]: https://mstksg.github.io/advent-of-code-2021/src/AOC.Challenge.Day05.html
407+
[d05r]: https://github.com/mstksg/advent-of-code-2021/blob/master/reflections-out/day05.md
408+
409+
*Reflection not yet written -- please check back later!*
410+
411+
### Day 5 Benchmarks
412+
413+
```
414+
>> Day 05a
415+
benchmarking...
416+
time 5.318 ms (5.256 ms .. 5.390 ms)
417+
0.998 RΒ² (0.997 RΒ² .. 0.999 RΒ²)
418+
mean 5.236 ms (5.194 ms .. 5.298 ms)
419+
std dev 149.2 ΞΌs (117.1 ΞΌs .. 207.5 ΞΌs)
420+
variance introduced by outliers: 12% (moderately inflated)
421+
422+
* parsing and formatting times excluded
423+
424+
>> Day 05b
425+
benchmarking...
426+
time 11.37 ms (11.19 ms .. 11.58 ms)
427+
0.997 RΒ² (0.995 RΒ² .. 0.999 RΒ²)
428+
mean 11.48 ms (11.36 ms .. 11.73 ms)
429+
std dev 417.9 ΞΌs (285.5 ΞΌs .. 608.1 ΞΌs)
430+
variance introduced by outliers: 13% (moderately inflated)
431+
432+
* parsing and formatting times excluded
433+
```
434+
435+
436+
392437
Day 6
393438
------
394439

β€Žsrc/AOC/Challenge/Day05.hsβ€Ž

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ day05b = day05 id
100100
-- solve y = (4-0)`div`2 = 2
101101
-- and so check if 2 is in range
102102

103+
-- sloped: <t, z+m*t>
104+
-- intersect with other slope:
105+
-- <t, z1 + m1 * t> == <t, z2 + m2 * t> (t = x = equal)
106+
-- solve: z1 + m1 * t == z2 + m2 * t
107+
-- m1 * t = m2 * t + (z2 - z1)
108+
-- t = (m2 * t + z2 - z1) / m1
109+
--
110+
103111
overlaps :: Line -> Line -> [V2 Integer]
104112
overlaps = \case
105113
Line Horiz y1 rx1 -> \case

β€Žsrc/AOC/Challenge/Day09.hsβ€Ž

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
-- will recommend what should go in place of the underscores.
2323

2424
module AOC.Challenge.Day09 (
25-
-- day09a
26-
-- , day09b
25+
day09a
26+
, day09b
2727
) where
2828

2929
import AOC.Prelude
@@ -47,14 +47,52 @@ import qualified Text.Megaparsec.Char.Lexer as PP
4747

4848
day09a :: _ :~> _
4949
day09a = MkSol
50-
{ sParse = Just . lines
50+
{ sParse = Just . parseAsciiMap (Just . digitToInt)
5151
, sShow = show
52-
, sSolve = Just
53-
}
52+
, sSolve = \xs -> Just . sum $ M.mapWithKey (go xs) xs
53+
}
54+
where
55+
go mp p i
56+
| and $ mapMaybe f (cardinalNeighbs p) = (i+1)
57+
| otherwise = 0
58+
where
59+
f q = case M.lookup q mp of
60+
Nothing -> Nothing
61+
Just j -> Just $ j > i
62+
-- cardinalNeighbs :: Point -> [Point]
63+
64+
65+
-- go xs = sum $ zipWith3 f xs (drop 1 xs) (drop 2 xs)
66+
-- where
67+
-- f a b c
68+
-- | a > b && c > b = 1 + b
69+
-- | otherwise = 0
5470

5571
day09b :: _ :~> _
5672
day09b = MkSol
57-
{ sParse = sParse day09a
73+
{ sParse = Just . parseAsciiMap (Just . digitToInt)
5874
, sShow = show
59-
, sSolve = Just
60-
}
75+
, sSolve = \xs -> Just $
76+
let lowpoints = M.keys $ M.filterWithKey (go xs) xs
77+
in product . take 3 . reverse . sort . map (S.size . floodFill (spreadout xs) . S.singleton) $ lowpoints
78+
}
79+
where
80+
go mp p i = and $ mapMaybe f (cardinalNeighbs p)
81+
where
82+
f q = case M.lookup q mp of
83+
Nothing -> Nothing
84+
Just j -> Just $ j > i
85+
spreadout mp p = S.fromList . filter valid . cardinalNeighbs $ p
86+
where
87+
valid q = case M.lookup q mp of
88+
Nothing -> False
89+
Just i -> i /= 9
90+
91+
92+
-- -- | Flood fill from a starting set
93+
-- floodFill
94+
-- :: Ord a
95+
-- => (a -> Set a) -- ^ Expansion (be sure to limit allowed points)
96+
-- -> Set a -- ^ Start points
97+
-- -> Set a -- ^ Flood filled
98+
-- floodFill f = snd . floodFillCount f

β€Žtest-data/2021/09a.txtβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2199943210
2+
3987894921
3+
9856789892
4+
8767896789
5+
9899965678
6+
>>> 15

β€Žtest-data/2021/09b.txtβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2199943210
2+
3987894921
3+
9856789892
4+
8767896789
5+
9899965678
6+
>>> 1134

0 commit comments

Comments
Β (0)