Skip to content

Commit 48441af

Browse files
committed
Add incomplete day 9 and complete day 10
1 parent 460174b commit 48441af

File tree

8 files changed

+406
-0
lines changed

8 files changed

+406
-0
lines changed

day10/Day10Part1.hs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import Data.List
2+
import Data.Maybe
3+
4+
openingChars = ['(', '[', '{', '<']
5+
closingChars = [')', ']', '}', '>']
6+
7+
scoreForChar :: Char -> Int
8+
scoreForChar ch
9+
| ch == ')' = 3
10+
| ch == ']' = 57
11+
| ch == '}' = 1197
12+
| ch == '>' = 25137
13+
| otherwise = 0
14+
15+
removeFirst :: Char -> String -> String
16+
removeFirst _ "" = ""
17+
removeFirst ch (a:acceptableNextChars)
18+
| a == ch = acceptableNextChars
19+
| otherwise = a : removeFirst ch acceptableNextChars
20+
21+
matching :: Char -> Char
22+
matching ch
23+
| ch `elem` openingChars = closingChars !! (fromJust $ elemIndex ch openingChars)
24+
| ch `elem` closingChars = openingChars !! (fromJust $ elemIndex ch closingChars)
25+
26+
parseLine :: String -> String -> Char
27+
parseLine acceptableClosingChars (ch:str)
28+
| str == [] = if ch `elem` acceptableClosingChars then ' ' else ch
29+
| ch `elem` openingChars = parseLine ((matching ch):acceptableClosingChars) str
30+
| ch == acceptableClosingChars!!0 = parseLine (removeFirst ch acceptableClosingChars) str
31+
| otherwise = ch
32+
33+
main = do
34+
contents <- readFile "day10/input.txt"
35+
let ls = lines contents
36+
print $ foldl (\acc ch -> acc + scoreForChar ch) 0 (map (parseLine "") ls)

day10/Day10Part2.hs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import Data.List
2+
import Data.Maybe
3+
4+
openingChars = ['(', '[', '{', '<']
5+
closingChars = [')', ']', '}', '>']
6+
7+
scoreForChar :: Char -> Int
8+
scoreForChar ch
9+
| ch == ')' = 1
10+
| ch == ']' = 2
11+
| ch == '}' = 3
12+
| ch == '>' = 4
13+
| otherwise = 0
14+
15+
removeFirst :: Char -> String -> String
16+
removeFirst _ "" = ""
17+
removeFirst ch (a:acceptableNextChars)
18+
| a == ch = acceptableNextChars
19+
| otherwise = a : removeFirst ch acceptableNextChars
20+
21+
matching :: Char -> Char
22+
matching ch
23+
| ch `elem` openingChars = closingChars !! (fromJust $ elemIndex ch openingChars)
24+
| ch `elem` closingChars = openingChars !! (fromJust $ elemIndex ch closingChars)
25+
26+
parseLine :: String -> String -> String
27+
parseLine acceptableClosingChars (ch:str)
28+
-- surely this is just one more iteration of the recursion? no time though
29+
| str == [] = if ch `elem` openingChars
30+
then (matching ch):acceptableClosingChars
31+
else if ch == acceptableClosingChars!!0
32+
then (removeFirst ch acceptableClosingChars)
33+
else ""
34+
| ch `elem` openingChars = parseLine ((matching ch):acceptableClosingChars) str
35+
| ch == acceptableClosingChars!!0 = parseLine (removeFirst ch acceptableClosingChars) str
36+
| otherwise = ""
37+
38+
score :: Int -> String -> Int
39+
score total (ch:[]) = total * 5 + scoreForChar ch
40+
score total (ch:str) = score (total * 5 + scoreForChar ch) str
41+
42+
main = do
43+
contents <- readFile "day10/input.txt"
44+
let incomplete = filter (/="") $ map (parseLine "") (lines contents)
45+
print $ (sort $ map (score 0) incomplete) !! (length incomplete `div` 2)

day10/input.txt

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
{<<[{(<<<([[<<{}()><<>{}>]{[(){}]{{}()}}]]<[{(()())<[]{}>}(({}[])(()[]))]>)>>{{[<[[(<><>)<{}()>]{(
2+
(({[(<{[[{{[{[<>[]][{}{}]}([{}()][<>()])][[{()()}(()<>)][([]){<>[]}]]}}{<{{{<>{}}(()<>)}[(
3+
<{({[(<{[[((({()()}<<>()>)){<{[]()}<[]()>><[()[]]>})<{[<[]{}>]<{{}[]}{()[]}>}<[[[]<>]]<(<>())(<>{})
4+
<{<[{[(<<(<(<[(){}]{()()}>)[{({}())[<>{}]}]>((<(()<>){<>()}>{{{}<>}([][])})))([({[{}()]<[]()
5+
<[<{(<{{{{[{[{[]{}}<()[]>](<[]{}><[][]>)}<<<<>{}>{<>()}>{([]<>)(()[])}>]}}(<[{[<[]()>[[]<>]]}]>([{
6+
{[<({<{{([{{{(()<>)(<>{})}}}{(([()()]{()())){[{}{}]([])})<<<()()><<>()>>((()()){[][]})>}](({{<<
7+
<{<[[(<(((<{[[()<>]{{}()}][(<>)[<>])}>)))>({{{{((<(){}>(()<>))){(<()<>>(()()))[<()<>><[]<>>]}
8+
<{<({(<(<{{<[((){})[()<>]]{{<>{}}([][])}>}[<{[[]()]}{<[]{}>(()<>)}>(<[()]<{}()>><{()<>}{[]()}>)]}
9+
{<<({{{(<{{<<[{}{})[(){}]>{<<><>><[]{}>}>({({}())[<>[]]}{{{}{}}<<><>>})}}({[[(()){(){}}]({[]()}<<>{}
10+
{{{<(([{<([[[[<><>][[]<>]](({}[])<{}{}>)][[<[][]>{{}<>}]<([]{})([]{})>]]{<[(())({}{})]<((){}){<>[]}
11+
[(<(<<[{{{<<[({}[])<<>>][{{}<>}<[]{}>]>([({}<>){{}[]}]{(<>{})})>}(<<<{()<>}{(){}}>(([]<>){{}[]})>
12+
{{<([({([<([[(()[])<[]()>][{{}{}}<<>()>]](({<>}{<>[]})<{[][]}{()[]}>))[[<({}[])<<>[]>>[<<>{}>({}<>)}][(<<><>>
13+
<<<<{{(<{<(<[<<><>>(<>[])]{{<>}((){})>>(({<>()}(<>{})){{{}{}}[(){}]}))<<[[()()][[]<>]]([<><>])><[<[]<>>[[
14+
<<[<(([(<([<({<><>}{[]<>}){(<>{})<<><>>}>({<<>[]>{()[]}}{(<>())[()[]]})])(<(<{[]<>}{[]{}}>{[()
15+
(<[<(({{{<(<<{[][]}([][])>[<{}{}>{{}<>}]><<<{}[]>[(){}]>>){{([()<>][{}<>])[<<>()>[<>[]]]>[{<[]{}>[[]{}]}[(
16+
<([<(<<{[{{{{{{}[]}{()<>}}([{}()]<{}()>)}{{({}())(<>())}[[<>()]<()()>]}}<{<[<>]>[[[]{}]({}{})]
17+
{[[[[{(<({[[<{{}()}(<>{})>{<<>{}>}]][<[<[][]><[]{}>](([][]))>({<[]()>[{}[]]}[[<>{}]<[]()>])]}(<<(<<>()>)[{<><
18+
({(([<<({<<<{([][]){<>()}}<[{}<>]{<>{}}>](<[()<>][[][]]>({()[]}[[][]]))>{[<[[]{}](()())>((<><>)<{}{}>)][
19+
{[(([{[(<[[(<{{}[]}{()()}]<({}<>)[<><>]>)[[[<>{}](<>())][[{}{}]<{}{}>]]]{<<({}[])[<>[]]>{{
20+
{{([<[{[<(([{({}{})({}[])}[{<>[]}[()()]]][([<>{}](<>))]))>{{([<<()()>(<><>)>((()[]))]]<<(<[]{}>[<>[]]
21+
{{{<[[<{[(<{{({}{})<<><>>}(<[]{}>[(){}])}({{[]()}(()<>)]<{[]<>}[<>[]]>)>[{((<>()){()()})((<><>)<<>>)}
22+
({{<<([<<[<([({}())([]<>)]{<(){}>})([[()()]])>]>><<[<(({<>{}})[(()<>){{}()}])([{()[]}[<><>]](<<>[]>[[]{}]
23+
([<<(<<({<<{(<()()>)[<<>()>[(){}]]}({(()<>)([][])})>>}({<[{{(){}}<{}{}>}<([]{}){[][]}>]>}))[[([[[([][]){(
24+
([[<([(<{{(({(<><>)<()>}[{()<>}({}<>)]))<([[()](()[])][{()<>}[<><>]])<<[[]{}]<()[]>><{<>[]}[{}[]]>>>][{{<<()[
25+
{[([<{((<[{{<{<>{}}><([][])<<>[]>>}<<[{}[]]>([(){}])>>{<([()[]]{{}()})(({}[]))><<{()()}<()[]>>
26+
<({<(<[{<[[{[{()<>}[()[]]]{[[][]]({}<>)}}<<([]())>>]{(<{<><>}<[]()>><{<>{}}>)[(<{}[]><<>()
27+
[{({[<{<(<<([{[]<>}([]())]{{(){}}})>{(<{<>{}}(<>{})>{{[]<>}([]())})({<<><>>}({()[]}<{}<>>))}>([
28+
<<<<<<<{<{{[[<[][]>{<>{}}]<<{}()>[[]{}]>]((([]{}))({<><>}({}<>)))}[<[<[][]>][[<>][{}[]]}>[{(()<>)}[<(){}
29+
([[{<{(<[{[([[{}{}][()()]](<{}[]>[<>[]])}]}(<<<((){})[<>()]><{{}[]}([][])>>>(<[<{}<>>{{}()}][{()()}(<><>)]>
30+
{[{[{([[[[<[([(){}]<{}<>>)[({}())(()[])]]>]<([<{{}<>}>[<<>>{()[]}]]{<<<>{})<<>{}>><<<>{}>(<><>)
31+
{<([<((((<[[{<<>{}>[<>()]}]<<(()<>)<()()>>[([]{})<(){}>]>][({<{}[]>}<[()<>](<>())))<<[[]()]<
32+
([[(<<{[{<[<<<<>[]><<>[]>>({[]<>}<<>{}>)>([<<><>>(()())]({{}()}))][[<([]<>)({})>{{[]()}[[][]]}]<([
33+
<[<[[([[([<[([(){}]<<>{}>)<(()())[{}{}]>]{({()()}{[][]})((()[])({}{}))}>[[{[{}{}](<><>)}{{[]{}}{
34+
[(<({<{(([{[<((){})>{{[][]}{()[]}}]}]{((<([]())<<>{}>>{{<>()}{[]}])){(<<{}<>><<>[]>>){({{}[]}<<>[]>)<[(){}]
35+
{((((<[[{<{({([]())[{}]}[<{}[]>{()}])}>}]{<[<<(<{}{}>(()[]))>[{{<><>}{[]{}}}<[[][]][()<>]>]>](<<{[[](
36+
<((({(<[[({({({}{}}{{}[]}})([{[]}({}[])]{[{}()]({}())})}{([{{}{}}(<>[])]<({}[])[<>()]>)[[<
37+
(([([[([<((<(<()()>({}()))[[<>]<<>[]>])[<([]<>)>(<()()>(<>{}))])<{[{[][]}(()<>)]}[[({}[])<()
38+
{{(((([<<(<{<[[]<>][<><>]><[{}()]<<>()>>}<{{{}()}([]{})}(<()<>><{}<>>)>>){<({[()[]](<>{})}[[<>()](()[])])<
39+
<(<<([<[[([(([[]()]<(){}>)[{{}()}[{}[]]])][<<<<>()>(<>)>{{()[]}[{}<>]}>])([[([<>[]]<<>()>){{<>()}<()<>>}][
40+
<<<([([{((([<{[]{}}<(){}>>[(()<>)[[]{}]]]<{<[]{}><[]{}>)>)<<[[{}{}]{{}()}][{()[]}[()()]]>([{[
41+
{(<<{<[<{<{{(<<>()>{{}[]})[{[]<>}{()[]}]}}(({({}<>)<()>}<[{}{}]{{}<>]>)({<{}{}>((){})}{{[][]}[[][]]
42+
{([[<[<[[(<([<()>{[]{}}]<[<>{}](()[])>){({{}}([][]))}>)[({(((){})<{}{}>)})[({({}[])[[]()]}<<{}<>>(<><>)>)
43+
[[<[[([([[{((<{}{}><<>()>)<<<>{}>((){}>>)<<<(){}><[]{}>>([[]{}])>}({[[<>{}]({}())]<<<><>>>})]]((<{{(
44+
[<{{[([{[[<((([]){(){}})<{<>[]}{[]{}}>){[[[]{}]{[]{}}]}>]{[([<()[]>[<>{}]](([][]){<>{}}))]}]}{
45+
({([{((<[([[<[{}()][()[]}>]{<{<>[]}<{}{}>>[<<>>{()<>}]}]<({{{}<>}{{}<>}}{<<>{}><()<>>})<(([]()
46+
({<(({{{{{<{{{[]()}<[]>}[{<>{}}[[][]]]}[{({}())({}())}{({}{})<[]{}>}]>}{[<(([]())({}<>))>[({{
47+
<<<{[[[<<{[<<((){})(()())>[[[]<>]<<><>>]>{{[{}{}]{(){}}}<{<><>](()())>}][(<{(){}}>)]}>(<[<[
48+
{[<[{<({([({(<<><>>){([][]){(){}}}}[{{()()}[<><>]}]]{<{<<>>(<><>)}{{<>[]}(<>)}>[{{[]<>}[<>[]]}((<>[]){[]})]}]
49+
<{[{{[<{[<<<[({}<>)[{}]][{{}{}}]>(({()[]}<{}<>})<[{}]([][])>)>>[[<<[()<>]({}[])>(<[][]>[{}()])>({{
50+
(({[<([({{<{{(()<>)[{}[]]}<{(){}}<<>()>>}(<[()()](<><>)>{[()()]([]<>)})>({(([]()){{}})<[[]()](<>{
51+
{{<<[<((<<({[(()<>)([]<>))({[]{}})}[([()[]]{[]{}})])<((<[]{}>(()())))>>{<<<<{}>(<><>)>>(<{<><>}>)>}>
52+
{{([{{[{([({[<{}<>>({}[])][({})<()<>>]}[<(<>())(<>{})>({(){}}<[]()>)])<{<<{}{}>>[<()()>(<>{})]}({[()<>
53+
(<(({{<<(<[{{{(){}}(<>[])}<(<>())>}{<{<><>}({}{})>{<[]{}><()[]>}}][<((()[])<{}<>>)((<><>){()()})>]
54+
<((({{{([([(<{()[]}[{}()]>)][[[[{}[]][<>{}]][{{}}[()()]]]([[{}<>]]{(<>())(()[])})])[{{(<{}()]<<>()>)[{<><>}[
55+
(([({[([({([[[[]{}]({})]{{[]{}}{{}()}}][[[<>{}]]<<()[]>[<>{}]>]){<<[[]()]<{}[]>>({(){}}<<>{}>)>
56+
(<{<[({(<({{{[[]<>][<>()]}}[{[[]()]{[]{}}}]}<(([[]()])<<<>[]>{[][]}>)[<([][]){<>{}}>([{}{}]{{}<>
57+
<{((([[{{{{[[{[]()}[<><>]]<[<><>][{}{}]>](<{{}}({}{})>{<{}>[()<>]})}{([<[][]>]({<>{}}<[][]>))<{[<>{}]}>}
58+
{<{<([<<[<<{<<<>{}>[()()]>{<[]()><()[]>}}[{([]<>>{[][]}}[({}[])([][])]]>{<[[<>[]]<{}[]>]<[{}[]][[][]]>>}>{{<
59+
([<[[[<[[<[{<<<>{}>[<>]>(([][]))}([<{}[]>{<>{}}][<{}[]>])]<{{(()[])[[]()]}}[{{{}[]}[{}<>]}(({}(
60+
[{<[{[<{<{(({{{}()}<[]{}>}{[[]()]})<{[<>[]][<><>]}<(()<>){[][]}>>)<(({{}()}<{}>){(()){[][]}})<{[()<>
61+
(<{([[{{(((([(()<>)](({}[])[()[]]))])([(([<>]<<>{}>)<{(){}}{<><>}>)[[{[]{}}<<><>>][({})[<>[]]]]]{{<({}{})<()
62+
{<{(<<<<{{{{[<()()>{(){}}](({})<()()>)}{<[[]{}]<<><>>>}}[<{[<>]{()<>}}[[[]()]]>]}({([[<>[]]{{}()}]){<<{}<>
63+
([[{<[[{[<{[<({})<<>[]>>{{[]<>}[[]{}]}]{([<>[]][[]{}])(<{}><()>)}}{<{{{}[]}<{}{}>}{<<><>>[[]()
64+
((<[<(({<{{{{<()<>>}((()())(<>{}))}}{[{({}[]){<>()}}[<()<>>]}}}[<[({()<>}{<>{}}){{<>()}[<><>]}]<(<(){}>(
65+
[({[(([({[({(<<>[]>)([{}<>]{()()})}<<(<>{})(()[])><[<>{}](()<>)>>)]<[[{(<>()){<>()}}{(<>()}{{}[]}}]]>}{[(([
66+
[{{<<(({<<<[[({}()){[]}][{(){}}]]>[[<{()<>}([]())>]]}((<<(<>())<()[]>>[(<>()){[][]}]>{[({})<()()>]}))
67+
[{((([{<<<[<<[{}]<[]()>><([][])[<>{}]>>{[<{}<>>{[]<>}]([<>[]])}](<(<{}[]>{{}()})>([(<>{})<[]<>>]{{<>[]}{<>
68+
(<[([<[[{{[(<(()())(<>{})><<{}[]>{<>[]}>)<<([]{}){[]}>{(()[])[<>]}>]({<{<>()}>[<<><>>[()[]]]}[(
69+
[<(<[([<[([<[[[]][()()]]{[[][]]{[][]}}>(<([]{})>)][<({[][]})<({}[])([]())>><{{[]()}}{<{}[]>)>])[{[<<()(
70+
{{<{[(({[[(((<<>()><<>})<(()()){[]()}>)(<{[][]}<[][]>>{[[][]]{[]}}))<[<{<>{}}>]>]{{[[{<>{}}[
71+
<{<[{{[<<[[{<{{}()}><[()()]([]{})>}][<[(()<>){<><>}]{[{}()]}>{[{()[]}[<>{}]]{(()[])[<>[]]}}]]<([{{<>()}{
72+
{<<[<[(<{([<{[<>{}][{}{}]}({[]()}(()))>(<([]{})><<<>{}><{}<>>>)][{{{[]{}}}[([]<>){[]{}}]}<(<<>
73+
{[(<{({(<<({(({}()){[]{}})<(()[])>}[((<>{})([]()))<{{}[]}[<>[]]>])<[<[{}()]{[][]}><<[]{}>>]>>{
74+
({(((<<{([{[[<<>{}>[[]()]][{<>[]}<()[]>]]}{({<[][]}{<>()}})({<{}<>>{{}<>}}<{[]{}}({}())>)}]
75+
[[{[{<<<[[([<<<><>>[()[]]><{<>[]}>])]{([[<<><>>[<><>]][{<>()}{{}{}}]]([{<>()}<{}[]>]{{[][]}([]<>)})){{(
76+
{<([[[[[[{([[<[][]><{}()>]<([][])<(){}>>]({[[][]]{()[]}})){({<{}{}><<><>>}){([{}{}]{<><>}){(
77+
({<[(((((<(<[[[]<>](<><>)]<{<>[]}[(){}]>>>(({[<>[]]<<>()>}{[()()]<{}<>>})[{<{}[]>[{}[]]}[[[](
78+
({{[(({({[({{<<>{}>((){})}(({}{}))})]<[<[({}{})((){})]<<<>{}>>><{[{}[]](<>{})}<<[]<>>([][]
79+
({([[<{(([<(((())[<>()])[[[]]<{}()>])<[<()<>>{(){}}][{()<>}[[]<>]]>><[<[<>{}]{<>()}>](<(<>())
80+
{((<<[({(((<{{{}{}}<<>{}>}<({}())<<>[]>>><([<>])<[[]()]>>)[[<<<>[]>(()<>)][([]())]]{{<[]<>>([]{})}<{{}()}(
81+
((([{{[(<{{((<{}{}><<><>])[({}<>)[[]<>]]){<{()[]}[[]()]>(([])[{}[]])}}(<[[[]{}][{}[]]][[()[
82+
[{{<{<(<[<{({<[]<>>}<<[][]>[()[]]>){<(<>()]{<>[]}>[<{}[]>{()()}]}}>{[[{[()()]<<>[]>}<[[]<>](<>
83+
<((<[<<[(<{[[(<>{})(<>{})]<{{}()}>](([{}{}][(){}]}[[<><>]{{}<>}])}<{[[{}{}][{}()]][(()())<[][]>]}>><<
84+
[(({[[{[{<{[(<<>[]><{}{}>)(([]{}){(){}})]{[[[][]]<<>()>]({<>()}(<>{}))}}[<{{<>()}([]{})}[{[]()}[()[]]]>[[
85+
[({[([{[({<<([<>[]])(<{}<>><{}{}>)>>{<[{[][]}<{}[]>){[<><>]<()()>}>[(<<>{}>{[][]})[{()<>}(<
86+
[<<<{[[({<{[{<()()><{}[]>}]<{(()())<{}{}>}{{<>{}}<<>()>}>}<{[{{}[]}(<>)]((<>())[()<>])}{<[<
87+
[[(<(({([<<<[<{}<>>(<>{})][{<>[]}([][])]><<<[]<>>(())>[[(){}][()<>]]>><{(<{}<>>[{}{}])<{()
88+
[<<{[([<([([(({}{})[<>[]])<{[]<>}(()())>]<<<{}[]>{<>{}}>{({}[]){{}<>}}>)(([[[][]]](<{}{}><[]{}>)){[<{}[]>{{}<
89+
[<{[({[{{<[([<<><>>[[][]]]([{}{}]({}{})))([[{}<>]<[]{}>][[<>()]{[][]}])]<<(<<>{}><<>{}>){[<>{}](<>[
90+
({[<[[{[(<<[<{[][]}([]<>)>[<<>[]>[()[]]]]<(<{}[]><<>{}>)[<[][]>]>>[[{{<><>}{<>}}<[{}()]{<><>}}]{(<<>{}>
91+
<<({[<[([{(([{(){}}][(()<>){{}<>>])<(<<>>)<[[]{}]{<>{}}>>)}<[<{<<>()>(<>())}>[{<[][]>(<>{})}[<{}[
92+
{(<(({(((({({[<><>]<[]()>}{{<>[]}{<>()}}){({{}()}{()()})((<><>><()[]>)}})<[[{({}())({}{})}[{<><>
93+
({(<({(<{((({([]{}){[]()}}({()<>}{(){}))){({<>[]}<[]{}>)<<[]()><[][]>>}))}>){(({[[[<<>[]>][<()[]>([][])]](
94+
([<{<{(([(<<([{}]([]{}))<(()())({})>>([<{}()>(<>())])>{[[[()()]]<[<>{}]<{}{}>>][<{(){}]{<>{}}><(<>[
95+
[<[{(({<({([[{[][]}({}[])]([{}()]<{}[]>)])((<<[]{})({}())>{(<>{}){{}()}})[[<<>>(())]])}[{[[[()()](<>[
96+
(<(((({(<{[{(<<>()>([]()))[<[]()>[<>]]}]<<[[()<>]([]{})]><(<[]{}>{<>[]}){{[]()}(()[])}>>}>)
97+
<(<<(<<[(([{<<[]{}>(()())>{[(){}]([][])}}][(<{{}{}}{[][]}>(([][])<<>[]>))]))[[<<([{}[]]<[]{}>)>[([()[]]<{}
98+
[<<<<(<(((<[(<()[]><[]{}>)](<{[]{}}[[]{}]>[<{}<>><<>{}>])>[[{{<>()}}([()<>][[]{}])]<(<[][]>((){}))<<[]
99+
[<<[([{([[({{{()()}({}{})}({()[]}{<>{}})}{[{<>[]}[<>()]]((<><>)({}()))}){[{<{}{}>(<>[])}{{{}<>}}][<([]{})
100+
[((({<<<<[<([{(){}}(()[])}{{[][]}({}{})})([{{}{}}{[]()}](([]<>){<><>}))><<<<{}[]>(<>[])>{(<>{})[<>
101+
{<[({<[<[([{<{[][]}<{}{}>>([{}>)}([{[][]}({}[])][[{}()]])]{[[[{}<>]<<>{}>]{<<><>><[][]>}][(
102+
{({<{[<{[<<[(<<>><<><>>)[<()[]>[{}[]]]]<[(<>[])<{}>]{({}[]){()[]}}>>{[<{[]()}({})>((())[()[]])](<({}<>)

day10/sample.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[({(<(())[]>[[{[]{<()<>>
2+
[(()[<>])]({[<{<<[]>>(
3+
{([(<{}[<>[]}>{[]{[(<()>
4+
(((({<>}<{<{<>}{[]{[]{}
5+
[[<[([]))<([[{}[[()]]]
6+
[{[{({}]{}}([{[{{{}}([]
7+
{<[[]]>}<{[{[{[]{()[[[]
8+
[<(<(<(<{}))><([]([]()
9+
<{([([[(<>()){}]>(<<{{
10+
<{([{{}}[<[[[<>{}]]]>[]]

day9/Day9Part1.hs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import Data.List
2+
import Data.Ix
3+
4+
type Coord = ( Int, Int )
5+
type Size = Coord
6+
data Matrix = Matrix { matrixData :: [ Int ]
7+
, width :: Int
8+
, height :: Int
9+
} deriving (Show)
10+
11+
parse :: [String] -> Matrix
12+
parse ls = Matrix {matrixData = concat $ [[read (item:"")::Int | item <- row] | row <- ls], width = length $ ls!!0, height = length ls}
13+
14+
matrixElem :: Coord -> Matrix -> Int
15+
matrixElem (x, y) matrix
16+
| x < 0 || y < 0 || x > (width matrix - 1) || y > (height matrix - 1) = -1
17+
| otherwise = (matrixData matrix) !! (y * (width matrix) + x)
18+
19+
20+
neighbours :: Coord -> Matrix -> [Int]
21+
neighbours (x, y) matrix = filter (>=0) [
22+
matrixElem (x - 1, y) matrix,
23+
matrixElem (x + 1, y) matrix,
24+
matrixElem (x, y - 1) matrix,
25+
matrixElem (x, y + 1) matrix
26+
]
27+
28+
lowerThanNeighbours :: Coord -> Matrix -> Bool
29+
lowerThanNeighbours coord matrix = all (\x -> x > matrixElem coord matrix) (neighbours coord matrix)
30+
31+
allCoords :: Matrix -> [Coord]
32+
allCoords matrix = [(x, y) | (y, x) <- range ((0,0), (height matrix - 1, width matrix - 1))]
33+
34+
lowPoints :: Matrix -> [Coord]
35+
lowPoints matrix = filter (\coord -> lowerThanNeighbours coord matrix ) (allCoords matrix)
36+
37+
lowPointValues :: Matrix -> [Int]
38+
lowPointValues matrix = [(matrixElem coord matrix) + 1 | coord <- lowPoints matrix]
39+
40+
main = do
41+
contents <- readFile "day9/input.txt"
42+
let matrix = parse $ lines contents
43+
print $ sum $ lowPointValues matrix

day9/Day9Part2.hs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
-- not working
2+
3+
import Data.List
4+
import Data.Ix
5+
6+
type Coord = ( Int, Int )
7+
type Size = Coord
8+
data Matrix = Matrix { matrixData :: [ (Coord, Int) ]
9+
, width :: Int
10+
, height :: Int
11+
} deriving (Show)
12+
13+
parse :: [String] -> Matrix
14+
parse ls = Matrix {
15+
matrixData = zip (allCoords (length $ ls!!0) (length ls)) (concat $ [[read (item:"")::Int | item <- row] | row <- ls]),
16+
width = length $ ls!!0,
17+
height = length ls
18+
}
19+
20+
allCoords :: Int -> Int -> [Coord]
21+
allCoords width height = [(x, y) | (y, x) <- range ((0,0), (height - 1, width - 1))]
22+
23+
matrixValue :: Coord -> Matrix -> (Coord, Int)
24+
matrixValue (x, y) matrix
25+
| x < 0 || y < 0 || x >= (width matrix) || y > (height matrix) = ((x, y), -1)
26+
| otherwise = (matrixData matrix) !! (y * (width matrix) + x)
27+
28+
neighbours :: Coord -> Matrix -> [(Coord, Int)]
29+
neighbours (x, y) matrix = filter (\(_, v) -> v >= 0) [
30+
matrixValue (x - 1, y) matrix,
31+
matrixValue (x + 1, y) matrix,
32+
matrixValue (x, y - 1) matrix,
33+
matrixValue (x, y + 1) matrix
34+
]
35+
36+
lowerThanNeighbours :: Coord -> Matrix -> Bool
37+
lowerThanNeighbours coord matrix = all (\(c, v) -> v > snd (matrixValue coord matrix)) (neighbours coord matrix)
38+
39+
lowPoints :: Matrix -> [Coord]
40+
lowPoints matrix = filter (\coord -> lowerThanNeighbours coord matrix ) [coord | (coord, _) <- matrixData matrix]
41+
42+
43+
-- basin lowPoint
44+
-- - take all neighbours
45+
-- - if not 9 and not in the list, add to the list and call basin for this element
46+
-- - if 9 add nothing
47+
48+
basinFrom :: Coord -> Matrix -> [Coord] -> [Coord]
49+
basinFrom coords matrix acc
50+
| value == 9 = acc
51+
| length newCoords > 0 = basinFrom (head newCoords) matrix (coords:acc ++ tail newCoords)
52+
| otherwise = coords:acc
53+
where ((x, y), value) = matrixValue coords matrix
54+
newCoords = [c | (c, v) <- neighbours coords matrix, c `notElem` acc, v /= 9]
55+
56+
main = do
57+
contents <- readFile "day9/sample.txt"
58+
let matrix = parse $ lines contents
59+
print $ matrix
60+
print $ lowPoints matrix
61+
62+
-- print $ [basinFrom coord matrix [] | coord <- lowPoints matrix]
63+
-- putStrLn $ "[]" ++ (show $ neighbours (0, 8) matrix)
64+
-- putStrLn $ "[((8, 0), 1), ((9, 1), 1)]" ++ (show $ neighbours (9, 0) matrix)
65+
-- print $ [neighbours n matrix | (n, _) <- neighbours (9, 0) matrix]

0 commit comments

Comments
 (0)