Skip to content

Commit 78aac18

Browse files
committed
first day
1 parent c992f7e commit 78aac18

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

data/examples/01.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
L68
2+
L30
3+
R48
4+
L5
5+
R60
6+
L55
7+
L1
8+
L99
9+
R14
10+
L82

data/examples/02.txt

Whitespace-only changes.

src/bin/01.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
advent_of_code::solution!(1);
2+
3+
pub fn part_one(input: &str) -> Option<u64> {
4+
let lines = input.lines();
5+
6+
let mut hitted_zero_times = 0;
7+
let mut current_state: i32 = 50;
8+
9+
for line in lines {
10+
let movement_direction = &line[0..1];
11+
let movement_count: i32 = line[1..].parse().unwrap();
12+
13+
match movement_direction {
14+
"L" => {
15+
current_state = (current_state - movement_count) % 100;
16+
}
17+
"R" => {
18+
current_state = (movement_count + current_state) % 100;
19+
}
20+
_ => {
21+
panic!("Nein")
22+
}
23+
}
24+
25+
if current_state == 0 {
26+
hitted_zero_times += 1
27+
}
28+
}
29+
30+
Some(hitted_zero_times)
31+
}
32+
33+
pub fn part_two(input: &str) -> Option<u64> {
34+
let lines = input.lines();
35+
36+
let mut hitted_zero_times: u64 = 0;
37+
let mut current_state: i64 = 50;
38+
39+
for line in lines {
40+
let movement_direction = &line[0..1];
41+
let movement_count: i64 = line[1..].parse().unwrap();
42+
43+
match movement_direction {
44+
"L" => {
45+
let first_hit = if current_state == 0 {
46+
100
47+
} else {
48+
current_state
49+
};
50+
let zeros = if movement_count >= first_hit {
51+
1 + (movement_count - first_hit) / 100
52+
} else {
53+
0
54+
};
55+
hitted_zero_times += zeros as u64;
56+
57+
current_state = ((current_state - movement_count) % 100 + 100) % 100;
58+
}
59+
"R" => {
60+
let first_hit = if current_state == 0 {
61+
100
62+
} else {
63+
100 - current_state
64+
};
65+
let zeros = if movement_count >= first_hit {
66+
1 + (movement_count - first_hit) / 100
67+
} else {
68+
0
69+
};
70+
hitted_zero_times += zeros as u64;
71+
72+
current_state = (current_state + movement_count) % 100;
73+
}
74+
_ => {
75+
panic!("Nein")
76+
}
77+
}
78+
}
79+
80+
Some(hitted_zero_times)
81+
}
82+
83+
#[cfg(test)]
84+
mod tests {
85+
use super::*;
86+
87+
#[test]
88+
fn test_part_one() {
89+
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
90+
assert_eq!(result, None);
91+
}
92+
93+
#[test]
94+
fn test_part_two() {
95+
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
96+
assert_eq!(result, None);
97+
}
98+
}

src/bin/02.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
advent_of_code::solution!(2);
2+
3+
pub fn part_one(input: &str) -> Option<u64> {
4+
None
5+
}
6+
7+
pub fn part_two(input: &str) -> Option<u64> {
8+
None
9+
}
10+
11+
#[cfg(test)]
12+
mod tests {
13+
use super::*;
14+
15+
#[test]
16+
fn test_part_one() {
17+
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
18+
assert_eq!(result, None);
19+
}
20+
21+
#[test]
22+
fn test_part_two() {
23+
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
24+
assert_eq!(result, None);
25+
}
26+
}

0 commit comments

Comments
 (0)