Get AOC day1 working

- Fixes to codegen for literal cases.
- Fix parsing of string literals
- Work around stack overflow in Prettier
This commit is contained in:
2024-10-22 20:30:20 -07:00
parent 9148852eb5
commit c7593e831e
13 changed files with 1298 additions and 38 deletions

94
aoc2023/Day1.newt Normal file
View File

@@ -0,0 +1,94 @@
module Day1
import Lib
ptype Int
-- TODO fix import of fixity declarations
infixr 4 _::_
infixl 3 _<_
infixl 4 _-_
infixl 4 _+_
infixl 5 _*_
infixl 5 _/_
infixr 0 _$_
-- Code
infixl 7 _._
_._ : {A B C : U} -> (B -> C) -> (A -> B) -> A -> C
(f . g) x = f ( g x)
digits1 : List Char -> List Int
digits1 Nil = Nil
digits1 (c :: cs) = let x = ord c in
case x < 58 of
True => case 48 < x of
True => x - 48 :: digits1 cs
False => digits1 cs
False => digits1 cs
-- This happens with Char and not Nat, but why is Char working at all?
-- I suspect it will fix if PatLit is implemented correctly
tail : {a : U} -> List a -> List a
tail Nil = Nil
tail (x :: xs) = xs
-- TODO I used @ patterns in Lean
digits2 : List Char -> List Int
digits2 xs = case xs of
('o' :: 'n' :: 'e' :: _) => 1 :: digits2 (tail xs)
('t' :: 'w' :: 'o' :: _) => 2 :: digits2 (tail xs)
('t' :: 'h' :: 'r' :: 'e' :: 'e' :: _) => 3 :: digits2 (tail xs)
('f' :: 'o' :: 'u' :: 'r' :: _) => 4 :: digits2 (tail xs)
('f' :: 'i' :: 'v' :: 'e' :: _) => 5 :: digits2 (tail xs)
('s' :: 'i' :: 'x' :: _) => 6 :: digits2 (tail xs)
('s' :: 'e' :: 'v' :: 'e' :: 'n' :: _) => 7 :: digits2 (tail xs)
('e' :: 'i' :: 'g' :: 'h' :: 't' :: _) => 8 :: digits2 (tail xs)
('n' :: 'i' :: 'n' :: 'e' :: _) => 9 :: digits2 (tail xs)
(c :: cs) => let x = ord c in
case x < 58 of
True => case 48 < x of
True => x - 48 :: digits2 cs
False => digits2 cs
False => digits2 cs
Nil => Nil
combine : List Int -> Int
combine Nil = 0
combine (x :: Nil) = x * 10 + x
combine (x :: y :: Nil) = x * 10 + y
combine (x :: y :: xs) = combine (x :: xs)
part1 : String -> (String -> List Int) -> Int
part1 text digits =
let lines = split (trim text) "\n" in
let nums = map combine $ map digits lines in
foldl _+_ 0 nums
infixl 3 _>>_
_>>_ : {A B : U} -> A -> B -> B
a >> b = b
runFile : String -> Dummy
runFile fn =
let text = readFile fn in
log fn >>
log "part1" >>
log (part1 text (digits1 . unpack)) >>
log "part2" >>
log (part1 text (digits2 . unpack)) >>
log ""
-- Argument is a hack to keep it from running at startup. Need to add IO
main : Int -> Dummy
main _ =
runFile "aoc2023/day1/eg.txt" >>
runFile "aoc2023/day1/eg2.txt" >>
runFile "aoc2023/day1/input.txt"