-
Notifications
You must be signed in to change notification settings - Fork 0
/
day22.rs
155 lines (138 loc) · 3.63 KB
/
day22.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
149
150
151
152
153
154
155
use std::collections::BTreeSet;
use std::collections::BTreeMap;
fn task1(input: &str, m: i32) -> i32 {
let mut nodes = BTreeSet::<(i32, i32)>::new();
let mut y = 0i32;
for l in input.lines() {
let mid = l.len() as i32 / 2;
let mut x = 0;
for c in l.chars() {
if c == '#' {
nodes.insert((x - mid, y - mid));
}
x += 1;
}
y += 1;
}
let mut dx = 0i32;
let mut dy = -1i32;
let mut posx = 0i32;
let mut posy = 0i32;
let mut nn = 0i32;
for _ in 0..m {
let infected = nodes.contains(&(posx, posy));
if infected {
//turn right
let tmp = dy;
dy = dx;
dx = -tmp;
nodes.remove(&(posx, posy));
} else {
//turn left
let tmp = dy;
dy = -dx;
dx = tmp;
nodes.insert((posx, posy));
nn += 1;
}
posx += dx;
posy += dy;
}
// for y in -40..40 {
// for x in -40..40 {
// if nodes.contains(&(x, y)) {
// print!("#");
// } else {
// print!(".");
// }
// }
// println!();
// }
nn
}
enum State {
Weakened,
Infected,
Flagged,
}
fn task2(input: &str, m: i32) -> i32 {
let mut nodes = BTreeMap::<(i32, i32), State>::new();
let mut y = 0i32;
for l in input.lines() {
let mid = l.len() as i32 / 2;
let mut x = 0;
for c in l.chars() {
if c == '#' {
nodes.insert((x - mid, y - mid), State::Infected);
}
x += 1;
}
y += 1;
}
let mut dx = 0i32;
let mut dy = -1i32;
let mut posx = 0i32;
let mut posy = 0i32;
let mut nn = 0i32;
for _i in 0..m {
match nodes.get(&(posx, posy)) {
Some(&State::Infected) => {
//turn right
let tmp = dy;
dy = dx;
dx = -tmp;
nodes.insert((posx, posy), State::Flagged);
}
Some(&State::Weakened) => {
//continue in the same direction
nodes.insert((posx, posy), State::Infected);
nn += 1;
}
Some(&State::Flagged) => {
//flip dir
dy = -dy;
dx = -dx;
nodes.remove(&(posx, posy));
}
None => {
//turn left
let tmp = dy;
dy = -dx;
dx = tmp;
nodes.insert((posx, posy), State::Weakened);
}
}
posx += dx;
posy += dy;
}
nn
}
fn main() {
let day22 = "##########..#.###...##..#
##....#...#....#..####.#.
#..#.##..#..##.###..#.###
.#.#.......####.....#.#..
...######....#.##########
##.#.....#.#####.#....###
#.####.#..#.#.#...#.#..##
#.##..#####..###..###.##.
#.####.#.##.##...#.#.#.##
#.#.#......##.##..###.#.#
#...#.#..#.##....#.##..##
.#.....##.##..#.####..##.
.#......#.#.########..###
##....###.#.#.###...##..#
..##.###....#.....#...#.#
....##...##...##.##.#..##
..#.#.#..#######..###..##
......#####.#####..#.#..#
.####.#......#..###..#.##
#....####.#..#.##.###.##.
####.#...##....###...#.#.
#####.#......#.#..###.##.
#.##.#..#..#..#.....#.#.#
#...#.#.##.#.####.#.#..#.
.##.##..#..###.##.....###";
println!("{}", task1(day22, 10000));
println!("{}", task2(day22, 10000000));
}