Skip to content

Commit 7cc7b5f

Browse files
committed
2015 day 1 part 1
1 parent a5fef29 commit 7cc7b5f

File tree

5 files changed

+59
-7
lines changed

5 files changed

+59
-7
lines changed

2015/day3-haskell/Main.hs

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ main :: IO ()
55
main = do
66
content <- readFile "input.txt"
77
let map = Map.empty
8-
let res = fn1 content map 0 0
8+
let res = fn1 content map (0, 0)
99
print res
10-
let res = fn2 content map 0 0 0 0 True
10+
let res = fn2 content map (0, 0) (0, 0) True
1111
print res
1212

13-
fn1 [] map _ _ = Map.size map
14-
fn1 (x:xs) map row col = fn1 xs map2 row2 col2
13+
fn1 [] map (_, _) = Map.size map
14+
fn1 (x:xs) map (row, col) = fn1 xs map2 (row2, col2)
1515
where
1616
map2 = Map.insert (row, col) True map
1717
(row2, col2) = transform x row col
1818

19-
fn2 [] map _ _ _ _ _ = Map.size map
20-
fn2 (x:xs) map sRow sCol rRow rCol sTurn =
21-
fn2 xs map2 sRow2 sCol2 rRow2 rCol2 sTurn2
19+
fn2 [] map (_, _) (_, _) _ = Map.size map
20+
fn2 (x:xs) map (sRow, sCol) (rRow, rCol) sTurn =
21+
fn2 xs map2 (sRow2, sCol2) (rRow2, rCol2) sTurn2
2222
where
2323
map2 =
2424
if sTurn

2016/day1-haskell/Justfile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build:
2+
ghcid --command="stack ghci Main.hs" --test=":main"

2016/day1-haskell/Main.hs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import Data.List.Split (splitOn)
2+
import System.IO
3+
4+
data Direction
5+
= North
6+
| South
7+
| East
8+
| West
9+
10+
main :: IO ()
11+
main = do
12+
content <- readFile "input.txt"
13+
let s = splitOn ", " content
14+
let res = fn1 s North 0 0
15+
print res
16+
17+
fn1 :: [String] -> Direction -> Int -> Int -> Int
18+
fn1 [] _ row col = abs row + abs col
19+
fn1 (x:xs) dir row col = do
20+
let (to, count) = parse x
21+
let dir2 = turn dir to
22+
let (row2, col2) = move dir2 count row col
23+
fn1 xs dir2 row2 col2
24+
25+
parse :: String -> (Char, Int)
26+
parse s = (head s, read (drop 1 s) :: Int)
27+
28+
turn North turn =
29+
case turn of
30+
'R' -> East
31+
'L' -> West
32+
turn West turn =
33+
case turn of
34+
'R' -> North
35+
'L' -> South
36+
turn East turn =
37+
case turn of
38+
'R' -> South
39+
'L' -> North
40+
turn South turn =
41+
case turn of
42+
'R' -> West
43+
'L' -> East
44+
45+
move North count row col = (row - count, col)
46+
move South count row col = (row + count, col)
47+
move East count row col = (row, col + count)
48+
move West count row col = (row, col - count)

2016/day1-haskell/input.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
R3, R1, R4, L4, R3, R1, R1, L3, L5, L5, L3, R1, R4, L2, L1, R3, L3, R2, R1, R1, L5, L2, L1, R2, L4, R1, L2, L4, R2, R2, L2, L4, L3, R1, R4, R3, L1, R1, L5, R4, L2, R185, L2, R4, R49, L3, L4, R5, R1, R1, L1, L1, R2, L1, L4, R4, R5, R4, L3, L5, R1, R71, L1, R1, R186, L5, L2, R5, R4, R1, L5, L2, R3, R2, R5, R5, R4, R1, R4, R2, L1, R4, L1, L4, L5, L4, R4, R5, R1, L2, L4, L1, L5, L3, L5, R2, L5, R4, L4, R3, R3, R1, R4, L1, L2, R2, L1, R4, R2, R2, R5, R2, R5, L1, R1, L4, R5, R4, R2, R4, L5, R3, R2, R5, R3, L3, L5, L4, L3, L2, L2, R3, R2, L1, L1, L5, R1, L3, R3, R4, R5, L3, L5, R1, L3, L5, L5, L2, R1, L3, L1, L3, R4, L1, R3, L2, L2, R3, R3, R4, R4, R1, L4, R1, L5

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ go get github.com/teivah/[email protected]
4242

4343
### 2023
4444

45+
* [Day 1](https://adventofcode.com/2016/day/1): [Haskell](2016/day1-haskell/Main.hs)
4546
* [Day 3](https://adventofcode.com/2015/day/3): [Haskell](2015/day3-haskell/Main.hs)
4647
* [Day 24](https://adventofcode.com/2023/day/24): [Go](2023/day24-go/main.go)
4748
* [Day 23](https://adventofcode.com/2023/day/23): [Go](2023/day23-go/main.go)

0 commit comments

Comments
 (0)