-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18.rs
133 lines (115 loc) · 3.74 KB
/
day18.rs
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
use std::collections::HashSet;
use std::fs;
use itertools::Itertools;
use regex::{Regex};
use crate::utils::toolbox::parse_i32;
pub(crate) fn day18() {
println!("{}", part_a(fs::read_to_string("input/2023/day18/input.txt").unwrap()));
println!("{}", part_b(fs::read_to_string("input/2023/day18/input.txt").unwrap()));
}
fn part_a(input: String) -> usize {
let re = Regex::new(r"^(U|D|R|L) (\d+) \((.+)\)$").unwrap();
let mut pos: (i32, i32) = (0, 0);
let mut rect: HashSet<(i32, i32)> = HashSet::new();
let mut stack = vec!();
let mut prev_dir = "X";
input.lines().for_each(|line| {
let caps = re.captures(line).unwrap();
let dir = caps.get(1).unwrap().as_str();
let num = parse_i32(caps.get(2));
let _color = caps.get(3).unwrap().as_str();
match dir {
"U" => {
for i in 1..=num {
rect.insert((pos.0 - i, pos.1));
}
pos = (pos.0 - num, pos.1);
}
"D" => {
if prev_dir == "R" {
stack.push((pos.0 + 1, pos.1 - 1));
}
for i in 1..=num {
rect.insert((pos.0 + i, pos.1));
}
pos = (pos.0 + num, pos.1);
}
"R" => {
for i in 1..=num {
rect.insert((pos.0, pos.1 + i));
}
pos = (pos.0, pos.1 + num);
}
"L" => {
for i in 1..=num {
rect.insert((pos.0, pos.1 - i));
}
pos = (pos.0, pos.1 - num);
}
_ => panic!("Unknown dir {}", dir)
}
prev_dir = dir;
});
while let Some(pos) = stack.pop() {
if !rect.insert(pos) { continue; }
stack.push((pos.0 - 1, pos.1));
stack.push((pos.0 + 1, pos.1));
stack.push((pos.0, pos.1 - 1));
stack.push((pos.0, pos.1 + 1));
}
rect.len()
}
fn part_b(input: String) -> i128 {
let mut pos: (i128, i128) = (0, 0);
let mut rect: Vec<(i128, i128)> = vec! {pos};
let mut sides = 0;
input.lines().for_each(|line| {
let (_, hex) = line.split_once("#").unwrap();
let num = i128::from_str_radix(hex[0..5].to_string().as_str(), 16).unwrap();
sides += num;
match hex[5..6].to_string().as_str() {
// UP
"3" => {
pos = (pos.0 - num, pos.1);
rect.push(pos);
}
// DOWN
"1" => {
pos = (pos.0 + num, pos.1);
rect.push(pos);
}
// RIGHT
"0" => {
pos = (pos.0, pos.1 + num);
rect.push(pos);
}
// LEFT
"2" => {
pos = (pos.0, pos.1 - num);
rect.push(pos);
}
dir => panic!("Unknown dir {}", dir)
}
});
// https://www.mathopenref.com/coordpolygonarea.html
let mut ans = 0;
rect.iter().tuple_windows().for_each(|((x1, y1), (x2, y2))| {
ans += (x1 * y2) - (y1 * x2);
});
(-ans / 2) + (sides / 2) + 1 // Add (0,0)
}
#[cfg(test)]
mod day18_tests {
use std::fs;
use crate::y2023::day18::{part_a, part_b};
#[test]
fn test_works() {
assert_eq!(62, part_a(fs::read_to_string("input/2023/day18/test.txt").unwrap()));
assert_eq!(952408144115, part_b(fs::read_to_string("input/2023/day18/test.txt").unwrap()));
}
#[test]
fn input_works() {
assert_eq!(33491, part_a(fs::read_to_string("input/2023/day18/input.txt").unwrap()));
assert_eq!(87716969654406, part_b(fs::read_to_string("input/2023/day18/input.txt").unwrap()));
}
}