Skip to content

Commit 940d1ae

Browse files
committed
AoC 2025 - Day 1
1 parent aa0a12f commit 940d1ae

File tree

8 files changed

+128
-0
lines changed

8 files changed

+128
-0
lines changed

aoc2025/Aoc.newt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module Aoc
2+
3+
import Prelude
4+
5+
-- `by` is the first argument for use in `map`
6+
nums' : String → String → List Int
7+
nums' by s = map stringToInt $ filter (_/=_ "") $ split (trim s) by
8+
9+
nums : String → List Int
10+
nums s = map stringToInt $ filter (_/=_ "") $ split (trim s) " "
11+
12+
indexOf? : ∀ a. {{Eq a}} → a → List a → Maybe Nat
13+
indexOf? {a} z xs = go Z z xs
14+
where
15+
go : Nat → a → List a → Maybe Nat
16+
go ix z Nil = Nothing
17+
go ix z (x :: xs) =
18+
if z == x then Just ix else go (S ix) z xs
19+
20+
-- TODO move to Aoc library
21+
Point : U
22+
Point = Int × Int
23+
24+
instance Add Point where
25+
(a,b) + (c,d) = (a + c, b + d)
26+
27+
instance Sub Point where
28+
(a,b) - (c,d) = (a - c, b - d)
29+
30+
31+
32+
-- instance Ord Point where
33+
-- (a,b) < (c,d) = a < c || a == c && b < d
34+
35+
-- instance Eq Point where
36+
-- (a,b) == (c,d) = a == c && b == d

aoc2025/Data

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../src/Data

aoc2025/Day1.newt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
module Day1
2+
3+
import Prelude
4+
import Node
5+
6+
parseLine : String → Maybe Int
7+
parseLine s = case unpack s of
8+
('L' :: rest) => Just $ 0 - stringToInt (pack rest)
9+
('R' :: rest) => Just $ stringToInt (pack rest)
10+
_ => Nothing
11+
12+
parse : String → List Int
13+
parse text = mapMaybe parseLine $ split (trim text) "\n"
14+
15+
part1 : List Int → Int
16+
part1 xs = go 0 50 xs
17+
where
18+
go : Int → Int → List Int → Int
19+
go acc pos Nil = acc
20+
go acc pos (x :: xs) =
21+
let pos = mod (x + pos) 100
22+
acc = if pos == 0 then acc + 1 else acc
23+
in go acc pos xs
24+
25+
-- This is uglier than I'd like
26+
part2 : List Int → Nat
27+
part2 xs = go Z 50 xs
28+
where
29+
go : Nat → Int → List Int → Nat
30+
go acc pos Nil = acc
31+
go acc pos (0 :: xs) = go acc pos xs
32+
go acc pos (x :: xs) =
33+
if x == 0 then go acc pos xs
34+
else if x <= -100 then go (S acc) pos (x + 100 :: xs)
35+
else if 100 <= x then go (S acc) pos (x - 100 :: xs)
36+
else if x + pos < 0 then go (if pos == 0 then acc else S acc) (x + pos + 100) xs
37+
else if x + pos == 0 then go (S acc) (x + pos) xs
38+
else if 100 <= x + pos then go (S acc) (x + pos - 100) xs
39+
else go acc (x + pos) xs
40+
41+
run : String → IO Unit
42+
run fn = do
43+
printLn fn
44+
text <- readFile fn
45+
let xs = parse text
46+
let p1 = part1 xs
47+
printLn "part1 \{show p1}"
48+
let p2 = part2 xs
49+
printLn "part2 \{show p2}"
50+
51+
main : IO Unit
52+
main = do
53+
run "aoc2025/day1/eg.txt"
54+
run "aoc2025/day1/input.txt"

aoc2025/DayXX.newt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module DayXX
2+
3+
import Prelude
4+
import Node
5+
import Aoc
6+
7+
run : String → IO Unit
8+
9+
main : IO Unit
10+
main = do
11+
run "aoc2025/dayXX/eg.txt"
12+
run "aoc2025/dayXX/input.txt"

aoc2025/Node.newt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Node
2+
3+
import Prelude
4+
5+
pfunc fs : JSObject := `require('fs')`
6+
pfunc getArgs : List String := `arrayToList(String, process.argv)`
7+
pfunc readFile uses (MkIORes) : (fn : String) -> IO String := `(fn) => (w) => Prelude_MkIORes(require('fs').readFileSync(fn, 'utf8'), w)`

aoc2025/Prelude.newt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../src/Prelude.newt

aoc2025/day1/eg.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
L68
2+
L30
3+
R48
4+
L5
5+
R60
6+
L55
7+
L1
8+
L99
9+
R14
10+
L82

aoc2025/mkday

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/zsh -e
2+
day=$1
3+
if [ ! -d day${day} -a ! -z "$1" ]; then
4+
echo Make Day ${day}
5+
mkdir day${day}
6+
sed "s/XX/$day/g" DayXX.newt > Day$day.newt
7+
fi

0 commit comments

Comments
 (0)