Skip to content

Commit 9407443

Browse files
committed
make day 2 Flix solution slightly nicer to look at
1 parent 8b292e2 commit 9407443

File tree

1 file changed

+58
-28
lines changed

1 file changed

+58
-28
lines changed

day02/solution.flix

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,87 @@
1-
type alias Bag = (Int32, Int32, Int32)
2-
type alias Game = (Int32, List[Bag])
1+
@Test
2+
def test_part1(): Bool =
3+
let lines =
4+
"Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green" ::
5+
"Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue" ::
6+
"Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red" ::
7+
"Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red" ::
8+
"Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green" :: Nil;
9+
let ans = List.filterMap(parseGame, lines) |> solvePart1;
10+
Assert.eq("8", ans)
11+
12+
@Test
13+
def test_part2(): Bool =
14+
let lines =
15+
"Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green" ::
16+
"Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue" ::
17+
"Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red" ::
18+
"Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red" ::
19+
"Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green" :: Nil;
20+
let ans = List.filterMap(parseGame, lines) |> solvePart2;
21+
Assert.eq("2286", ans)
22+
23+
type alias Bag = { red = Int32, green = Int32, blue = Int32 }
24+
type alias Game = { gameId = Int32, bags = List[Bag] }
325

426
def parseBag(s: String): Bag =
5-
let colors = s
6-
|> String.splitOn({substr = ", "})
7-
|> List.map(String.breakOnLeft({substr = " "}))
8-
|> List.map(x -> (String.sliceRight({start=1}, snd(x)), Result.getWithDefault(0, Int32.parse(10, fst(x)))))
9-
|> List.toMap;
10-
(
11-
Map.getWithDefault("red", 0, colors),
12-
Map.getWithDefault("green", 0, colors),
13-
Map.getWithDefault("blue", 0, colors)
14-
)
27+
let cubeCounts = s
28+
|> String.splitOn({substr = ", "})
29+
|> List.map(String.breakOnLeft({substr = " "}))
30+
|> List.map(x -> {
31+
let cubeColor = String.trimLeft(snd(x));
32+
let count = Result.getWithDefault(0, Int32.parse(10, fst(x)));
33+
(cubeColor, count)
34+
})
35+
|> List.toMap;
36+
{
37+
red = Map.getWithDefault("red", 0, cubeCounts),
38+
green = Map.getWithDefault("green", 0, cubeCounts),
39+
blue = Map.getWithDefault("blue", 0, cubeCounts)
40+
}
1541

1642
def parseGame(s: String): Option[Game] =
1743
match String.splitOn({substr = ": "}, s) {
18-
case gameTitle :: rest :: Nil => {
44+
case gameTitle :: bagsStr :: Nil => {
1945
let gameNum = gameTitle
2046
|> String.sliceRight({start = 5})
2147
|> Int32.parse(10)
2248
|> Result.getWithDefault(0);
23-
let bags = rest
49+
let bags = bagsStr
2450
|> String.splitOn({substr = "; "})
2551
|> List.map(parseBag);
26-
Some((gameNum, bags))
52+
Some({ gameId = gameNum, bags = bags })
2753
}
2854
case _ => None
2955
}
3056

3157
def possibleBag(bag: Bag): Bool =
32-
let (r, g, b) = bag;
58+
let {red = r, green = g, blue = b} = bag;
3359
(r <= 12) `Bool.and` lazy (g <= 13) `Bool.and` lazy (b <= 14)
3460

35-
def possibleGame(g: Game): Bool =
36-
let bags = snd(g);
37-
List.forAll(possibleBag, bags)
38-
3961
def solvePart1(games: List[Game]): String =
4062
games
41-
|> List.filter(possibleGame)
42-
|> List.map(fst)
63+
|> List.filter(game -> {
64+
let {gameId = _, bags = bags} = game;
65+
List.forAll(possibleBag, bags)
66+
})
67+
|> List.map(game -> {
68+
let { gameId = gameId, bags = _ } = game;
69+
gameId
70+
})
4371
|> List.sum
4472
|> ToString.toString
4573

4674
def minimumBagPower(game: Game): Int32 =
47-
let bags = snd(game);
48-
let (r, g, b) =
49-
bags
50-
|> List.foldLeft((acc, bag) -> {
75+
let { gameId = _, bags = bags } = game;
76+
let (r, g, b) = List.foldLeft(
77+
(acc, bag) -> {
5178
let (maxR, maxG, maxB) = acc;
52-
let (r, g, b) = bag;
79+
let {red = r, green = g, blue = b} = bag;
5380
(Int32.max(maxR, r), Int32.max(maxG, g), Int32.max(maxB, b))
54-
}, (0, 0, 0));
81+
},
82+
(0, 0, 0),
83+
bags
84+
);
5585
r * g * b
5686

5787

0 commit comments

Comments
 (0)