-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06.rs
154 lines (124 loc) · 3.47 KB
/
06.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
use std::fs::File;
use std::io::{BufReader, BufRead};
use std::path::Path;
fn read_file<P>(filename: P) -> Result<Vec<Vec<char>>, std::io::Error>
where
P: AsRef<Path>
{
let file = File::open(filename)?;
let reader = BufReader::new(file);
let mut lines = Vec::new();
for line in reader.lines() {
lines.push(line?.chars().collect::<Vec<char>>());
}
Ok(lines)
}
fn p1() {
let filename = "input";
let mut lines = read_file(filename).expect("Failed to read file");
let mut x = 0;
let mut y = 0;
for (j, line) in lines.iter().enumerate() {
for (i, c) in line.iter().enumerate() {
if *c == '^' {
x = i;
y = j;
break;
}
}
}
let mut sum = 1;
lines[y][x] = 'X';
let dirs = [(0, -1), (1, 0), (0, 1), (-1, 0)];
let mut curr_dir = 0;
loop {
let (dx, dy) = dirs[curr_dir];
let next_x = (x as isize) + dx;
let next_y = (y as isize) + dy;
if next_x >= 0 && next_y >= 0 && next_x < lines[0].len() as isize && next_y < lines.len() as isize {
match lines[next_y as usize][next_x as usize] {
'#' => {
curr_dir = (curr_dir + 1) % 4;
}
'.' | '^' => {
sum += 1;
lines[next_y as usize][next_x as usize] = 'X';
x = next_x as usize;
y = next_y as usize;
}
_ => {
x = next_x as usize;
y = next_y as usize;
}
}
} else {
break;
}
}
println!("{}", sum);
}
fn p2() {
let filename = "input";
let lines = read_file(filename).expect("Failed to read file");
let guard_pos = {
let mut x = 0;
let mut y = 0;
for (j, line) in lines.iter().enumerate() {
for (i, c) in line.iter().enumerate() {
if *c == '^' {
x = i;
y = j;
break;
}
}
}
(x, y)
};
let mut sum = 0;
for i in 0..lines.len() {
for j in 0..lines[i].len() {
if lines[i][j] == '.' {
println!("{}, {}", i, j);
let mut grid = lines.clone();
grid[i][j] = '#';
if loops(&grid, guard_pos) {
sum += 1;
}
}
}
}
println!("{}", sum);
}
fn loops(lines: &Vec<Vec<char>>, guard_pos: (usize, usize)) -> bool {
let (mut x, mut y) = guard_pos;
let dirs = [(0, -1), (1, 0), (0, 1), (-1, 0)];
let mut dir = 0;
const MAX_TURNS: usize = 1000;
let mut turns = 0;
loop {
let (dx, dy) = dirs[dir];
if turns == MAX_TURNS {
return true;
}
let next_x = (x as isize) + dx;
let next_y = (y as isize) + dy;
if next_x >= 0 && next_y >= 0 && next_x < lines[0].len() as isize && next_y < lines.len() as isize {
match lines[next_y as usize][next_x as usize] {
'#' => {
dir = (dir + 1) % 4;
turns += 1;
}
_ => {
x = next_x as usize;
y = next_y as usize;
}
}
} else {
return false;
}
}
}
fn main() {
p1();
p2();
}