Skip to content

Commit b1da31e

Browse files
committed
Day 7 part 2
1 parent 1dae1ae commit b1da31e

File tree

3 files changed

+30
-37
lines changed

3 files changed

+30
-37
lines changed

src/adventofcode2024.gleam

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ fn run(d: Int) -> Nil {
4848
4 -> day4.part2(input) |> int.to_string
4949
5 -> day5.part2(input) |> int.to_string
5050
6 -> day6.part2(input) |> int.to_string
51+
7 -> day7.part2(input) |> int.to_string
5152
_ -> "(not implemented)"
5253
}
5354
})

src/day7.gleam

+25-37
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
import gleam/int
2-
import gleam/io
32
import gleam/list
43
import gleam/pair
54
import gleam/result
65
import gleam/string
76
import parse
87

98
pub fn part1(input: String) -> Int {
9+
solve(input, [Add, Multiply])
10+
}
11+
12+
pub fn part2(input: String) -> Int {
13+
solve(input, [Add, Multiply, Concatenate])
14+
}
15+
16+
fn solve(input: String, operations: List(Op)) -> Int {
17+
let valid = fn(e: #(Int, List(Op))) { is_valid(e, operations) }
18+
1019
input
1120
|> parse_input
12-
|> list.filter(is_valid)
21+
|> list.filter(valid)
1322
|> list.map(pair.first)
1423
|> int.sum
1524
}
@@ -18,6 +27,7 @@ pub type Op {
1827
Number(Int)
1928
Add
2029
Multiply
30+
Concatenate
2131
}
2232

2333
fn evaluate(ops: List(Op)) -> Int {
@@ -28,60 +38,38 @@ fn forward_evaluate(ops: List(Op)) -> Int {
2838
case ops {
2939
[Number(a), Add, ..rest] -> a + forward_evaluate(rest)
3040
[Number(a), Multiply, ..rest] -> a * forward_evaluate(rest)
41+
[Number(a), Concatenate, ..rest] -> {
42+
a
43+
|> int.digits(10)
44+
|> result.unwrap([])
45+
|> list.fold(forward_evaluate(rest), fn(acc, digit) { acc * 10 + digit })
46+
}
3147
[Number(a)] -> a
3248
_ -> panic as { "Invalid expression" }
3349
}
3450
}
3551

36-
fn is_valid(equation: #(Int, List(Op))) -> Bool {
37-
let _debug_equation = fn(ops: List(Op)) {
38-
io.println_error("")
39-
io.println_error(
40-
"checking if "
41-
<> int.to_string(equation.0)
42-
<> " == "
43-
<> debug_ops(ops)
44-
<> case evaluate(ops) == equation.0 {
45-
True -> " (yes!)"
46-
False -> " (no, " <> int.to_string(evaluate(ops)) <> ")"
47-
},
48-
)
49-
ops
50-
}
51-
52-
possible_ops(equation.1)
53-
// |> list.map(debug_equation)
52+
fn is_valid(equation: #(Int, List(Op)), operations: List(Op)) -> Bool {
53+
possible_ops(equation.1, operations)
5454
|> list.find(fn(ops) { evaluate(ops) == equation.0 })
5555
|> result.is_ok
5656
}
5757

58-
fn possible_ops(ops: List(Op)) -> List(List(Op)) {
58+
fn possible_ops(ops: List(Op), operations: List(Op)) -> List(List(Op)) {
5959
case ops {
6060
[Number(a)] -> [[Number(a)]]
6161
[Number(a), ..rest] -> {
62-
possible_ops(rest)
62+
possible_ops(rest, operations)
6363
|> list.flat_map(fn(restops) {
64-
[[Number(a), Add, ..restops], [Number(a), Multiply, ..restops]]
64+
operations
65+
|> list.map(fn(new_op) { [Number(a), new_op, ..restops] })
6566
})
6667
}
6768
[] -> []
68-
[Add, ..] | [Multiply, ..] ->
69-
panic as "possible_ops() requires numeric operands only"
69+
_ -> panic as "possible_ops() requires numeric operands only"
7070
}
7171
}
7272

73-
fn debug_ops(ops: List(Op)) -> String {
74-
ops
75-
|> list.map(fn(op) {
76-
case op {
77-
Number(a) -> int.to_string(a)
78-
Add -> "+"
79-
Multiply -> "*"
80-
}
81-
})
82-
|> string.join(" ")
83-
}
84-
8573
fn parse_input(input: String) -> List(#(Int, List(Op))) {
8674
input
8775
|> parse.lines

test/day7_test.gleam

+4
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ const example1 = "190: 10 19
2121
pub fn part1_test() {
2222
day7.part1(example1) |> should.equal(3749)
2323
}
24+
25+
pub fn part2_test() {
26+
day7.part2(example1) |> should.equal(11_387)
27+
}

0 commit comments

Comments
 (0)