feat: solve day 1
This commit is contained in:
parent
72dd89c2ee
commit
b36e0b4b6e
2 changed files with 81 additions and 0 deletions
11
data/examples/01.txt
Normal file
11
data/examples/01.txt
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
L68
|
||||||
|
L30
|
||||||
|
R48
|
||||||
|
L5
|
||||||
|
R60
|
||||||
|
L55
|
||||||
|
L1
|
||||||
|
L99
|
||||||
|
R14
|
||||||
|
L82
|
||||||
|
|
||||||
70
src/bin/01.rs
Normal file
70
src/bin/01.rs
Normal file
|
|
@ -0,0 +1,70 @@
|
||||||
|
advent_of_code::solution!(1);
|
||||||
|
|
||||||
|
pub fn part_one(input: &str) -> Option<u64> {
|
||||||
|
let mut rot = 50;
|
||||||
|
let mut num = 0;
|
||||||
|
input.lines().for_each(|l| {
|
||||||
|
let mut mult = 1;
|
||||||
|
if l.contains("L") {
|
||||||
|
mult = -1;
|
||||||
|
}
|
||||||
|
let mut chars = l.chars();
|
||||||
|
chars.next();
|
||||||
|
let curr = chars.as_str().parse().unwrap_or(0);
|
||||||
|
rot += mult * curr;
|
||||||
|
while rot >= 100 {
|
||||||
|
rot -= 100;
|
||||||
|
}
|
||||||
|
while rot < 0 {
|
||||||
|
rot += 100;
|
||||||
|
}
|
||||||
|
if rot == 0 {
|
||||||
|
num += 1;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Some(num)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn part_two(input: &str) -> Option<u64> {
|
||||||
|
let mut new_lines = vec![];
|
||||||
|
input.lines().for_each(|l| {
|
||||||
|
let mut chars = l.chars();
|
||||||
|
let start = chars.next();
|
||||||
|
let curr = chars.as_str().parse().unwrap_or(0);
|
||||||
|
if let Some(start_char) = start {
|
||||||
|
for _ in 0..curr {
|
||||||
|
new_lines.push(format!("{start_char}1"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
part_one(&new_lines.join("\n"))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_part_one() {
|
||||||
|
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
|
||||||
|
assert_eq!(result, Some(3));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_part_two() {
|
||||||
|
let mut result = part_two(&advent_of_code::template::read_file("examples", DAY));
|
||||||
|
assert_eq!(result, Some(6));
|
||||||
|
|
||||||
|
result = part_two("L1000");
|
||||||
|
assert_eq!(result, Some(10));
|
||||||
|
|
||||||
|
result = part_two("L50");
|
||||||
|
assert_eq!(result, Some(1));
|
||||||
|
result = part_two("R50\nR50");
|
||||||
|
assert_eq!(result, Some(1));
|
||||||
|
result = part_two("R50\nL50");
|
||||||
|
assert_eq!(result, Some(1));
|
||||||
|
result = part_two("L50\nL50");
|
||||||
|
assert_eq!(result, Some(1));
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue