-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.gleam
123 lines (106 loc) · 2.97 KB
/
solution.gleam
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import gleam/int
import gleam/io
import gleam/list
import gleam/pair
import gleam/result
import gleam/string
import internal/aoc_utils
pub fn main() {
let filename = "inputs/day07.txt"
let lines_result = aoc_utils.read_lines(from: filename)
case lines_result {
Ok(lines) -> {
// If the file was converting into a list of lines
// successfully then run each part of the problem
aoc_utils.run_part_and_print("Part 1", fn() { solve_p1(lines) })
aoc_utils.run_part_and_print("Part 2", fn() { solve_p2(lines) })
}
Error(_) -> io.println("Error reading file")
}
}
// Part 1
pub fn solve_p1(lines: List(String)) -> Result(String, String) {
use parsed_lines <- result.map({
lines
|> list.map(parse)
|> result.all
})
parsed_lines
|> list.filter(fn(parsed_line) { is_possible(parsed_line.0, parsed_line.1) })
|> list.map(pair.first)
|> int.sum
|> int.to_string
}
// Part 2
pub fn solve_p2(lines: List(String)) -> Result(String, String) {
use parsed_lines <- result.map({
lines
|> list.map(parse)
|> result.all
})
parsed_lines
|> list.filter(fn(parsed_line) {
is_possible_part2(parsed_line.0, parsed_line.1)
})
|> list.map(pair.first)
|> int.sum
|> int.to_string
}
fn parse(line: String) -> Result(#(Int, List(Int)), String) {
let first_split = case string.split(line, ": ") {
[pre, post] -> Ok(#(pre, post))
_ -> Error("Unable to parse line: " <> line)
}
use #(pre, post) <- result.try(first_split)
use target <- result.try({
int.parse(pre)
|> result.replace_error("Unable to convert " <> pre <> " to integer")
})
let parsed_list =
post
|> string.split(" ")
|> list.map(int.parse)
|> result.all
|> result.replace_error("Unable to parse integers in list " <> post)
use parsed_list <- result.try(parsed_list)
Ok(#(target, parsed_list))
}
fn is_possible(target: Int, values: List(Int)) -> Bool {
case values {
[] -> False
[first, ..rest] -> is_possible_acc(target, rest, first)
}
}
fn is_possible_acc(target: Int, values: List(Int), current: Int) -> Bool {
case values {
[] -> current == target
_ if current > target -> False
[first, ..rest] -> {
is_possible_acc(target, rest, current * first)
|| is_possible_acc(target, rest, current + first)
}
}
}
fn is_possible_part2(target: Int, values: List(Int)) -> Bool {
case values {
[] -> False
[first, ..rest] -> is_possible_part2_acc(target, rest, first)
}
}
fn is_possible_part2_acc(target: Int, values: List(Int), current: Int) -> Bool {
case values {
[] -> current == target
_ if current > target -> False
[first, ..rest] -> {
is_possible_part2_acc(target, rest, concat(current, first))
|| is_possible_part2_acc(target, rest, current * first)
|| is_possible_part2_acc(target, rest, current + first)
}
}
}
fn concat(x: Int, y: Int) -> Int {
let xs = int.to_string(x)
let ys = int.to_string(y)
let assert Ok(r) = int.parse(xs <> ys)
r
}