-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8.rs
148 lines (138 loc) · 3.85 KB
/
day8.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use crate::Input;
use str_block::str_block;
pub fn inputs() -> Vec<Input> {
vec![
Input::Hashed("8b278ec0816291e953d8e2e60113c4f1c1b6d76634ce8a64d9eda1f7702cfd9d"),
Input::Inline(
"example",
str_block! {"
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
"},
Some(14),
Some(34),
),
]
}
struct Antennae {
map: [(u8, [(i8, i8); 4]); 0x50],
width: u8,
height: u8,
}
impl Default for Antennae {
fn default() -> Self {
Self {
map: [(0, [(0, 0); 4]); 0x50],
width: 0,
height: 0,
}
}
}
impl Antennae {
fn parse(input: &str) -> Self {
let mut map = Self::default();
for line in input.as_bytes()[..input.len() - 1].split(|&b| b == b'\n') {
map.width = 0;
for b in line.iter().copied() {
if b >= b'0' {
let m = unsafe { map.map.get_unchecked_mut((b - b'0') as usize) };
m.1[m.0 as usize] = (map.width as i8, map.height as i8);
m.0 += 1;
}
map.width += 1;
}
map.height += 1;
}
map
}
#[inline(always)]
fn in_range(&self, x: i8, y: i8) -> bool {
(x as u8) < self.width && (y as u8) < self.height
}
}
struct LocMap([u64; 0x40]);
impl Default for LocMap {
fn default() -> Self {
Self([0; 0x40])
}
}
impl LocMap {
#[inline(always)]
fn set(&mut self, x: i8, y: i8) -> bool {
let (i, m) = (y as usize, 1 << x as u8);
let c = unsafe { self.0.get_unchecked_mut(i) };
let new = *c & m == 0;
*c |= m;
new
}
}
pub fn part1(input: &str) -> u32 {
let map = Antennae::parse(input);
let mut anti = LocMap::default();
let mut count = 0;
for locs in map.map {
let locs = &locs.1[..locs.0 as usize];
for (i, (ax, ay)) in locs.iter().copied().enumerate() {
for (bx, by) in locs[i + 1..].iter().copied() {
let dx = ax - bx;
let dy = ay - by;
let a1x = ax + dx;
let a1y = ay + dy;
let a2x = bx - dx;
let a2y = by - dy;
if map.in_range(a1x, a1y) {
count += anti.set(a1x, a1y) as u32;
}
if map.in_range(a2x, a2y) {
count += anti.set(a2x, a2y) as u32;
}
}
}
}
count
}
pub fn part2(input: &str) -> u32 {
let map = Antennae::parse(input);
let mut anti = LocMap::default();
let mut count = 0;
for locs in map.map {
let locs = &locs.1[..locs.0 as usize];
for (i, (ax, ay)) in locs.iter().copied().enumerate() {
for (bx, by) in locs[i + 1..].iter().copied() {
let dx = ax - bx;
let dy = ay - by;
let mut hx = ax;
let mut hy = ay;
loop {
count += anti.set(hx, hy) as u32;
hx += dx;
hy += dy;
if !map.in_range(hx, hy) {
break;
}
}
let mut hx = bx;
let mut hy = by;
loop {
count += anti.set(hx, hy) as u32;
hx -= dx;
hy -= dy;
if !map.in_range(hx, hy) {
break;
}
}
}
}
}
count
}