Skip to content

Commit 06a1c2f

Browse files
committed
Day3
1 parent 4604dad commit 06a1c2f

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

aoc2025/Day3.newt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
module Day3
2+
3+
import Prelude
4+
import Node
5+
6+
parse : String → List (List Int)
7+
parse text = map mkbank $ split (trim text) "\n"
8+
where
9+
mkbank : String → List Int
10+
mkbank line = map (\c => ord c - 48) $ unpack line
11+
12+
part1 : List (List Int) → Int
13+
part1 banks = foldl _+_ 0 $ map (max 0 0) banks
14+
where
15+
max : Int → Int → List Int → Int
16+
max a b Nil = a * 10 + b
17+
max a b (c :: Nil) = if c > b then max a c Nil else max a b Nil
18+
max a b (c :: cs) = if c > a then max c 0 cs else if c > b then max a c cs else max a b cs
19+
20+
twelve : Nat
21+
twelve = cast 12
22+
23+
part2 : List (List Int) → Int
24+
part2 banks = foldl _+_ 0 $ map go banks
25+
where
26+
calc : Int → List Int → Int
27+
calc acc Nil = acc
28+
calc acc (x :: xs) = calc (10 * acc + x) xs
29+
30+
try : List Int → Int → List Int
31+
try (a :: b :: cs) n = if a < b then b :: cs ++ (n :: Nil) else a :: try (b :: cs) n
32+
try (a :: Nil) n = if a < n then n :: Nil else a :: Nil
33+
try Nil n = Nil
34+
35+
go : List Int → Int
36+
go xs =
37+
let cand = take twelve xs
38+
rest = drop twelve xs
39+
in calc 0 $ foldl try cand rest
40+
41+
run : String -> IO Unit
42+
run fn = do
43+
putStrLn fn
44+
text <- readFile fn
45+
let banks = parse text
46+
putStrLn $ "part1 " ++ show (part1 banks)
47+
putStrLn $ "part2 " ++ show (part2 banks)
48+
49+
main : IO Unit
50+
main = do
51+
run "aoc2025/day3/eg.txt"
52+
run "aoc2025/day3/input.txt"

aoc2025/day3/eg.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
987654321111111
2+
811111111111119
3+
234234234234278
4+
818181911112111

0 commit comments

Comments
 (0)