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
|
import gleam/bit_array
import gleam/list
import gleam/int
import gleam/io
import gleam/string
import simplifile
pub type Location {
Internal
Passenger
Other
}
pub type Packet {
Packet(length: Int, endpoint: Location)
}
pub fn main() {
let assert Ok(data) = simplifile.read(from: "./src/day2/data.txt")
let #(internal_packets, passenger_packets) =
data
|> string.split("\n")
|> list.map(parse_packet)
|> list.partition(fn(p) { p.endpoint == Internal })
{
int.to_string(sum_packet_lengths(internal_packets))
<> "/"
<> int.to_string(sum_packet_lengths(passenger_packets))
}
|> io.println()
}
fn parse_packet(raw_packet: String) {
let assert Ok(<<
_:bytes-size(2),
length:int-size(16),
_:bytes-size(8),
source:bytes-size(4),
destination:bytes-size(4),
>>) = bit_array.base16_decode(raw_packet)
Packet(length, set_endpoint(source, destination))
}
fn set_endpoint(source: BitArray, dest: BitArray) -> Location {
case identify_location(source), identify_location(dest) {
Other, Internal | Internal, Other -> Internal
Other, Passenger | Passenger, Other -> Passenger
_, _ -> Other
}
}
fn identify_location(ip: BitArray) -> Location {
case ip {
<<192, 168, _, _>> -> Internal
<<10, 0, _, _>> -> Passenger
_ -> Other
}
}
fn sum_packet_lengths(packets: List(Packet)) -> Int {
use acc, p <- list.fold(packets, 0)
acc + p.length
}
|