Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d59cf65

Browse files
ChAoSUnItYweihanglo
authored andcommitted
Add Day 06 solution
1 parent 7d9136a commit d59cf65

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed

‎2024/06/ChAoS_UnItY/Cargo.toml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "ChAoS_UnItY"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]

‎2024/06/ChAoS_UnItY/Day06.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<Your test data here>

‎2024/06/ChAoS_UnItY/LICENSE‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../01/ChAoS_UnItY/LICENSE

‎2024/06/ChAoS_UnItY/README.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Day 6 Solution
2+
3+
The part 2 solution could be some kind of perfect algorithm to solve with,
4+
however, it's quite hard to come up with atm, so I decided to just adopt
5+
brute force: by trying inserting obstacles on the path and test if the
6+
turning position and direction duplicated, if so, it's a loop.

‎2024/06/ChAoS_UnItY/src/main.rs‎

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
use std::collections::HashSet;
2+
3+
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
4+
enum Direction {
5+
Up,
6+
Right,
7+
Down,
8+
Left,
9+
}
10+
11+
impl Direction {
12+
fn get_vector(&self) -> (i32, i32) {
13+
match self {
14+
Direction::Up => (0, -1),
15+
Direction::Right => (1, 0),
16+
Direction::Down => (0, 1),
17+
Direction::Left => (-1, 0),
18+
}
19+
}
20+
21+
fn rotate_clockwise(&mut self) {
22+
*self = match self {
23+
Direction::Up => Direction::Right,
24+
Direction::Right => Direction::Down,
25+
Direction::Down => Direction::Left,
26+
Direction::Left => Direction::Up,
27+
}
28+
}
29+
}
30+
31+
fn main() {
32+
let content = include_str!("../Day06.txt");
33+
let path = part1(&content);
34+
part2(&content, path);
35+
}
36+
37+
fn parse_data(content: &str) -> ((i32, i32), Vec<(i32, i32)>) {
38+
let line_iter = content.lines().enumerate();
39+
let mut initial_pos = (0, 0);
40+
let mut obstacles = Vec::new();
41+
42+
for (y, line) in line_iter {
43+
for (x, c) in line.chars().enumerate() {
44+
match c {
45+
'^' => initial_pos = (x as i32, y as i32),
46+
'.' => {}
47+
'#' => obstacles.push((x as i32, y as i32)),
48+
_ => unreachable!(),
49+
}
50+
}
51+
}
52+
53+
(initial_pos, obstacles)
54+
}
55+
56+
fn part1(content: &str) -> HashSet<(i32, i32)> {
57+
let mut direction = Direction::Up;
58+
let line_cnt = content.lines().count() as i32;
59+
let (x_size, y_size) = (content.len() as i32 / line_cnt, line_cnt);
60+
let ((mut x, mut y), obstacles) = parse_data(content);
61+
let mut distinct_pos = HashSet::new();
62+
63+
loop {
64+
distinct_pos.insert((x, y));
65+
let (mut vx, mut vy) = direction.get_vector();
66+
let (mut nx, mut ny) = (x + vx, y + vy);
67+
68+
if obstacles.contains(&(nx, ny)) {
69+
direction.rotate_clockwise();
70+
(vx, vy) = direction.get_vector();
71+
(nx, ny) = (x + vx, y + vy);
72+
}
73+
74+
if nx < 0 || nx >= x_size || ny < 0 || ny >= y_size {
75+
// Out of bound
76+
break;
77+
}
78+
79+
(x, y) = (nx, ny);
80+
}
81+
82+
println!("Part 1: {}", distinct_pos.len());
83+
distinct_pos
84+
}
85+
86+
fn part2(content: &str, path: HashSet<(i32, i32)>) {
87+
let mut options = 0;
88+
let line_cnt = content.lines().count() as i32;
89+
let (x_size, y_size) = (content.len() as i32 / line_cnt, line_cnt);
90+
let ((gx, gy), obstacles) = parse_data(content);
91+
let mut obstacles: HashSet<(i32, i32)> = obstacles.into_iter().collect();
92+
93+
for (nox, noy) in path {
94+
obstacles.insert((nox, noy));
95+
let mut turns = HashSet::new();
96+
let (mut x, mut y) = (gx, gy);
97+
let mut direction = Direction::Up;
98+
99+
loop {
100+
let (vx, vy) = direction.get_vector();
101+
let (nx, ny) = (x + vx, y + vy);
102+
103+
if obstacles.contains(&(nx, ny)) {
104+
if turns.contains(&((nx, ny), direction)) {
105+
options += 1;
106+
break;
107+
}
108+
109+
turns.insert(((nx, ny), direction));
110+
direction.rotate_clockwise();
111+
continue;
112+
}
113+
114+
if nx < 0 || nx >= x_size || ny < 0 || ny >= y_size {
115+
// Out of bound
116+
break;
117+
}
118+
119+
(x, y) = (nx, ny);
120+
}
121+
122+
obstacles.remove(&(nox, noy));
123+
}
124+
125+
println!("Part 2: {options}");
126+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /