Skip to content

Commit 6a006e0

Browse files
committed
Add day6 part1
1 parent 1c30fad commit 6a006e0

File tree

3 files changed

+225
-0
lines changed

3 files changed

+225
-0
lines changed

input/2024/day6.txt

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
............................#....................................................#...#........................#.#......#........#.

src/day6.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
use aoc_runner_derive::{aoc, aoc_generator};
2+
3+
#[derive(Debug)]
4+
pub struct Map {
5+
map: Vec<Vec<char>>,
6+
current: (usize, usize),
7+
}
8+
9+
#[aoc_generator(day6)]
10+
pub fn input_generator(input: &str) -> Map {
11+
Map {
12+
map: input.lines().map(|l| l.chars().collect()).collect(),
13+
current: input
14+
.lines()
15+
.enumerate()
16+
.find_map(|(row, line)| line.chars().position(|c| c == '^').map(|col| (row, col)))
17+
.unwrap(),
18+
}
19+
}
20+
21+
enum Direction {
22+
Up,
23+
Down,
24+
Left,
25+
Right,
26+
}
27+
28+
impl Direction {
29+
// Clockwise 90-degree rotation
30+
fn rotate(self) -> Self {
31+
match self {
32+
Direction::Up => Direction::Right,
33+
Direction::Right => Direction::Down,
34+
Direction::Down => Direction::Left,
35+
Direction::Left => Direction::Up,
36+
}
37+
}
38+
}
39+
40+
#[aoc(day6, part1)]
41+
pub fn part1(input: &Map) -> usize {
42+
let mut direction: Direction = Direction::Up;
43+
let (mut y, mut x) = input.current;
44+
let mut new_map = input.map.clone();
45+
let mut count = 0;
46+
while y < input.map.len() && x < input.map[y].len() {
47+
if input.map[y][x] == '#' {
48+
// put back
49+
match direction {
50+
Direction::Up => y += 1,
51+
Direction::Down => y -= 1,
52+
Direction::Left => x += 1,
53+
Direction::Right => x -= 1,
54+
}
55+
direction = direction.rotate();
56+
} else if new_map[y][x] != 'X' {
57+
count += 1;
58+
new_map[y][x] = 'X';
59+
}
60+
match direction {
61+
Direction::Up => y -= 1,
62+
Direction::Down => y += 1,
63+
Direction::Left => x -= 1,
64+
Direction::Right => x += 1,
65+
}
66+
}
67+
count
68+
}
69+
70+
#[cfg(test)]
71+
mod tests {
72+
use super::*;
73+
74+
const INPUT: &str = "....#.....
75+
.........#
76+
..........
77+
..#.......
78+
.......#..
79+
..........
80+
.#..^.....
81+
........#.
82+
#.........
83+
......#...";
84+
85+
#[test]
86+
fn test_part1() {
87+
assert_eq!(41, part1(&input_generator(INPUT)))
88+
}
89+
90+
// #[test]
91+
// fn test_part2() {
92+
// assert_eq!(123, part2(&input_generator(INPUT)))
93+
// }
94+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ mod day2;
55
mod day3;
66
mod day4;
77
mod day5;
8+
mod day6;
89

910
aoc_lib! { year = 2024 }

0 commit comments

Comments
 (0)