Skip to content

Commit 2b37de2

Browse files
committed
day 3
1 parent 18af0bd commit 2b37de2

File tree

2 files changed

+391
-1
lines changed

2 files changed

+391
-1
lines changed

data/day3.txt

Lines changed: 323 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,323 @@
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+
.........................#..#.#
156+
#...#..#..##.###.....##.##.#...
157+
...#..................#.#....#.
158+
......#..##.#.......#.......#..
159+
.##....#.#................#....
160+
.#...#..#.#.#....##....#.......
161+
.##......#.....#..........#....
162+
..#...........#..##.........#..
163+
....#.#...........#..........##
164+
....#.#.#...........#.#........
165+
......#.....#..#....##....##...
166+
............##...##......#.#.##
167+
#.#.....#..#....#..#...#.#...#.
168+
.#...###..#..#.......#.......#.
169+
.....#..#.##.....#....#...#....
170+
##.....#..##.......##..#.#.#..#
171+
....#.#......##....#.....#..###
172+
.#...#.#......#.##...#..##.....
173+
.#...#...#......##..#..#...#.#.
174+
.#.........#....##...###...##..
175+
###.....#......####.....#.#....
176+
.....#..##.##................#.
177+
.#.................#...#..##.#.
178+
....#....#..#.......#.....#....
179+
.##....#..#..#.....###.#..#..#.
180+
#.#.......#.....##...#.....#...
181+
#.#........#.#.###...#....#....
182+
.#.....#.....##.#...#..#.......
183+
..###.#............#...##.###..
184+
.....#.....#..#..##............
185+
.#.#..#.#..##..#....#...##.....
186+
.#...........#..#.......#...#.#
187+
#.#.#.#.....##....#............
188+
...#.................#.#......#
189+
.....##.............#...#.#....
190+
.##......#.#....#..........#.#.
191+
.#.##.......##...#...#.....#.#.
192+
#...#.#........#......##....#.#
193+
#....##....#....#...#..#..#.#.#
194+
......#..........#...#.....#..#
195+
#..#....#....#..##.#..#.#...#..
196+
......#..#.#....#.....#.#..#..#
197+
...#.#...###........#.#......##
198+
..#............................
199+
...#.#..##...##...#...#......##
200+
...#.####......#.........#....#
201+
.#...#.#...##....#......#.#....
202+
.#.....##..##.#................
203+
.#...............#.............
204+
......#.....#...#..##..##......
205+
...#..##.......#.......#..#.#.#
206+
......##.....#..#.....#...#.#.#
207+
........##........#.#........##
208+
.#....#.....###..#.......#...#.
209+
#...#....#.........#.......#...
210+
...##..#........#####.#........
211+
###..#....#.#..#...#.####......
212+
..#..........#.#.............#.
213+
#......#.#....#.#.#....#.##....
214+
.#.#.#.............#....#...#..
215+
......#.....#.#...#..###.#..#..
216+
.....#..#............#...#...##
217+
..#......###..#........#.#.....
218+
#..##......#.#.#.#...........#.
219+
#..#...##.##.....#....#..#.....
220+
...##.#..........#.#....#...#..
221+
.#.#.#.#..#.#...#......#.......
222+
....#......###.#...............
223+
.........#...#....#...#.#....#.
224+
##.#.........#...##............
225+
........#..........#.#...#.....
226+
..#........#....#.......#......
227+
#..#...............#..#...##.#.
228+
#........#.....##.#..#....#...#
229+
..##....#....#.#...........##..
230+
....#.#.........#..#.....#..#..
231+
.......##....#.#.#....###.#....
232+
......#....#.#...#..#.........#
233+
.....##..#....#.#......#.#.#...
234+
#.##..##.#.......#..#...##.#.##
235+
........#.#..#...##.#.#..#.....
236+
#..#......#......#...#.#..#....
237+
.....#......#.#....##....##....
238+
....#.##...##..#..........##.#.
239+
.#....#.......#.........#......
240+
.#.......#.#...#...............
241+
....#.##.......#.##..#.##..#...
242+
#..#.......#.....#..#..........
243+
..#.##.......#....#.#..##..#...
244+
.#.....#...##.#.#..#...#.......
245+
.......#.........#......#.#....
246+
#.##.....##.......#....#.......
247+
##.#.#.........##..#.....#....#
248+
....#.#.#.#....#..#..##.......#
249+
#...#...........#.#............
250+
...#...#.#..#..##..............
251+
......#.......#.........#..#.#.
252+
#.....##.#....#...#..#.........
253+
#...#..###.##..###...##.....#..
254+
#....#.#.#...#.#..........#....
255+
................#.#....#.....##
256+
#.##..............####.....#.##
257+
................#.....#........
258+
#...#..#......#.....#......#...
259+
.........##...........#...#...#
260+
#.#....#...##.....#.....#..#..#
261+
.....#...##..##.............#..
262+
....###.#.......#.........#...#
263+
..#.......#......#..#...#.#....
264+
#.#....#......#.##....#.##.#...
265+
.#.#...#.......#.#...#.##..#...
266+
..........#......#.....#.......
267+
........#...#.....#...##...#.#.
268+
.....##....#.##..#........#.##.
269+
..........##.....#..#........#.
270+
.#....#..#.......#.##..........
271+
.#..#..#...#...#........#.##...
272+
.#...#.##.......#...#........#.
273+
.....#....#.............#..#...
274+
...#....##...#...#.....##......
275+
#.#####.........##...#.....#...
276+
......#.......#....#.....#..#..
277+
..#..............#.#..#..#.....
278+
....#.................#...#....
279+
###.#..##.#....#...#.#......#.#
280+
..##......#.#........#.#...##..
281+
.....#...#...#..#.#..#..##..#..
282+
.##...#......#...#...##.#...#..
283+
.......###.#...........##.##...
284+
.#.##..#.#.###.......#..##...#.
285+
..#....#.......#..##......#....
286+
.#....#.#..#..#.#.#....#...#...
287+
..........##....#....#.#.......
288+
.....#.......#.#..###.#.###....
289+
.#.#....#.##..#.#..#.....#.#.#.
290+
....#.....#.#.#............#...
291+
.###....#...##......##..###..#.
292+
...#.#..#.....#...#....##..#...
293+
.#.#....#..........#...##.....#
294+
#.....##...#........#.#..##..#.
295+
.......#....#.#..........#...#.
296+
.........#..#.#.###.........##.
297+
..................#.#....#....#
298+
....#....#.#..#.......###.##.##
299+
....#...#.................#....
300+
...#..#####.......#.#..##.##...
301+
##.#....#...............#..#...
302+
....#..........#...........#.#.
303+
..##.#.##.#..#.#....#..........
304+
.....#....#....##.#....#....#.#
305+
.......#..##.....###...#....#.#
306+
.#.......#..#.#.#...........#..
307+
.#...........##.#.##....#.#....
308+
....#.#....#.#.#......##.......
309+
.........##......#.#.....###...
310+
........#.#...#.##.....#.##.##.
311+
##.#..##.#.........#....#......
312+
.#.#.#....#..........#.#....#..
313+
....###.........#.#.#..........
314+
#..#....##.....#...............
315+
#.##....#.#...#.....#......#.#.
316+
............#.##........#......
317+
.....#.#.....##..##............
318+
.##..........#.......#......#..
319+
...##..##......#.....#..#....##
320+
.##.##...#.................##..
321+
#....#.#........#..#....#..##.#
322+
....##..##......#....###.#.#..#
323+
.....#....#..#..#...##...#...#.

src/main.rs

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,74 @@ fn test_day2_part1() {
150150
);
151151
}
152152

153+
fn day3_calc(map: &Vec<String>, right: usize, down: usize) -> usize {
154+
let mut cnt: usize = 0;
155+
let mut n: usize = 0;
156+
for i in (0..map.len()).step_by(down) {
157+
if i == 0 {
158+
continue;
159+
}
160+
n = n + 1;
161+
let row: &String = &map[i];
162+
let p: usize = n * right;
163+
if row.chars().nth(p % row.len()).unwrap_or('\0') == '#' {
164+
cnt = cnt + 1;
165+
}
166+
}
167+
return cnt;
168+
}
169+
170+
#[test]
171+
fn test_day3_calc() {
172+
let map: Vec<String> = vec![
173+
String::from("..##......."),
174+
String::from("#...#...#.."),
175+
String::from(".#....#..#."),
176+
String::from("..#.#...#.#"),
177+
String::from(".#...##..#."),
178+
String::from("..#.##....."),
179+
String::from(".#.#.#....#"),
180+
String::from(".#........#"),
181+
String::from("#.##...#..."),
182+
String::from("#...##....#"),
183+
String::from(".#..#...#.#"),
184+
];
185+
assert_eq!(2, day3_calc(&map, 1, 1));
186+
assert_eq!(7, day3_calc(&map, 3, 1));
187+
assert_eq!(3, day3_calc(&map, 5, 1));
188+
assert_eq!(4, day3_calc(&map, 7, 1));
189+
assert_eq!(2, day3_calc(&map, 1, 2));
190+
}
191+
fn day3_part1(map: &Vec<String>) -> usize {
192+
return day3_calc(map, 3, 1);
193+
}
194+
195+
fn day3_part2(map: &Vec<String>) -> usize {
196+
let a1 = day3_calc(map, 1, 1);
197+
let a2 = day3_calc(map, 3, 1);
198+
let a3 = day3_calc(map, 5, 1);
199+
let a4 = day3_calc(map, 7, 1);
200+
let a5 = day3_calc(map, 1, 2);
201+
return a1 * a2 * a3 * a4 * a5;
202+
}
203+
204+
fn day3() {
205+
println!("day3");
206+
207+
let mut map: Vec<String> = Vec::new();
208+
if let Ok(lines) = read_lines("data/day3.txt") {
209+
for line in lines {
210+
if let Ok(v) = line {
211+
map.push(v);
212+
}
213+
}
214+
}
215+
println!("{}", day3_part1(&map));
216+
println!("{}", day3_part2(&map));
217+
}
218+
153219
fn main() {
154220
// day1();
155-
day2();
221+
//day2();
222+
day3();
156223
}

0 commit comments

Comments
 (0)