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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
import gleam/io
import gleam/int
import gleam/list
import gleam/bool
import gleam/pair
import gleam/string as str
import gleam/dict.{type Dict}
import ext/resultx as resx
import ext/genericx as genx
import util/input_util
import util/parser as p
type Range {
Range(min: Int, max: Int)
}
type Rule =
List(Range)
type Ticket =
List(Int)
type Notes {
Notes(
fields: Dict(String, Rule),
your_ticket: Ticket,
nearby_tickets: List(Ticket),
)
}
fn parse_notes(input: String) -> Notes {
let range_parser =
p.int()
|> p.skip(p.literal("-"))
|> p.then(p.int())
|> p.map2(with: Range)
|> p.labeled(with: "range")
let rule_parser =
range_parser
|> p.sep1(by: p.literal(" or "))
|> p.labeled(with: "rule")
let field_parser =
p.str_of_many1(of: p.gc_not_in(":"))
|> p.skip(p.literal(": "))
|> p.then(rule_parser)
|> p.labeled(with: "field")
let ticket_parser =
p.int()
|> p.sep1(by: p.literal(","))
|> p.labeled(with: "ticket")
let notes_parser =
field_parser
|> p.sep1(by: p.nl())
|> p.map(with: dict.from_list)
|> p.skip(p.nlnl())
|> p.skip(p.literal("your ticket:"))
|> p.skip(p.nl())
|> p.then(ticket_parser)
|> p.skip(p.nlnl())
|> p.skip(p.literal("nearby tickets:"))
|> p.skip(p.nl())
|> p.then_3rd(p.sep1(ticket_parser, by: p.nl()))
|> p.skip_ws()
|> p.map3(with: Notes)
|> p.labeled(with: "notes")
input
|> p.parse_entire(with: notes_parser)
|> resx.assert_unwrap
}
fn satisfies_range(value: Int, range: Range) -> Bool {
range.min <= value && value <= range.max
}
fn satisfies_rule(value: Int, rule: Rule) -> Bool {
list.any(in: rule, satisfying: satisfies_range(value, _))
}
type Column {
Collapsed(String)
Pending(List(String))
}
fn collapse_columns(columns: List(Column)) -> List(String) {
case
list.find_map(in: columns, with: fn(column) {
case column {
Pending([name]) -> Ok(name)
_ -> Error(Nil)
}
})
{
Ok(target) ->
columns
|> list.map(with: fn(column) {
case column {
Collapsed(name) -> Collapsed(name)
Pending([_]) -> Collapsed(target)
Pending(names) ->
names
|> list.filter(keeping: genx.different(_, than: target))
|> Pending
}
})
|> collapse_columns
Error(Nil) ->
columns
|> list.map(with: fn(column) {
let assert Collapsed(name) = column
name
})
}
}
fn part1(input: String) -> Int {
let notes = parse_notes(input)
notes.nearby_tickets
|> list.flatten
|> list.filter(keeping: fn(value) {
notes.fields
|> dict.values
|> list.any(satisfying: satisfies_rule(value, _))
|> bool.negate
})
|> int.sum
}
fn part2(input: String) -> Int {
let notes = parse_notes(input)
[notes.your_ticket, ..notes.nearby_tickets]
|> list.filter(keeping: fn(ticket) {
list.all(in: ticket, satisfying: fn(value) {
notes.fields
|> dict.values
|> list.any(satisfying: satisfies_rule(value, _))
})
})
|> list.transpose
|> list.map(with: fn(column) {
notes.fields
|> dict.to_list
|> list.filter_map(with: fn(entry) {
let #(name, rule) = entry
case list.all(in: column, satisfying: satisfies_rule(_, rule)) {
True -> Ok(name)
False -> Error(Nil)
}
})
|> Pending
})
|> collapse_columns
|> list.strict_zip(notes.your_ticket)
|> resx.assert_unwrap
|> list.filter(fn(entry) {
let #(column, _) = entry
str.starts_with(column, "departure")
})
|> list.map(with: pair.second)
|> int.product
}
pub fn main() -> Nil {
let testing = input_util.read_text("test16")
let assert 71 = part1(testing)
let assert 1 = part2(testing)
let input = input_util.read_text("day16")
io.debug(part1(input))
io.debug(part2(input))
Nil
}
|