y2016d1 genuinely don't understand why this was so hard
This commit is contained in:
parent
f663a57db8
commit
b39f312e9f
1
y2016/resources/1_input.txt
Normal file
1
y2016/resources/1_input.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
L5, R1, L5, L1, R5, R1, R1, L4, L1, L3, R2, R4, L4, L1, L1, R2, R4, R3, L1, R4, L4, L5, L4, R4, L5, R1, R5, L2, R1, R3, L2, L4, L4, R1, L192, R5, R1, R4, L5, L4, R5, L1, L1, R48, R5, R5, L2, R4, R4, R1, R3, L1, L4, L5, R1, L4, L2, L5, R5, L2, R74, R4, L1, R188, R5, L4, L2, R5, R2, L4, R4, R3, R3, R2, R1, L3, L2, L5, L5, L2, L1, R1, R5, R4, L3, R5, L1, L3, R4, L1, L3, L2, R1, R3, R2, R5, L3, L1, L1, R5, L4, L5, R5, R2, L5, R2, L1, L5, L3, L5, L5, L1, R1, L4, L3, L1, R2, R5, L1, L3, R4, R5, L4, L1, R5, L1, R5, R5, R5, R2, R1, R2, L5, L5, L5, R4, L5, L4, L4, R5, L2, R1, R5, L1, L5, R4, L3, R4, L2, R3, R3, R3, L2, L2, L2, L1, L4, R3, L4, L2, R2, R5, L1, R2
|
20
y2016/src/bin/d1.rs
Normal file
20
y2016/src/bin/d1.rs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
use std::fs;
|
||||||
|
|
||||||
|
use y2016::days::d1;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
part1();
|
||||||
|
part2();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part1() {
|
||||||
|
let root = env!("CARGO_MANIFEST_DIR");
|
||||||
|
let content = fs::read_to_string(format!("{root}/resources/1_input.txt")).unwrap();
|
||||||
|
println!("{}", d1::process_part1(&content));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part2() {
|
||||||
|
let root = env!("CARGO_MANIFEST_DIR");
|
||||||
|
let content = fs::read_to_string(format!("{root}/resources/1_input.txt")).unwrap();
|
||||||
|
println!("{}", d1::process_part2(&content));
|
||||||
|
}
|
105
y2016/src/days/d1.rs
Normal file
105
y2016/src/days/d1.rs
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
use core::panic;
|
||||||
|
use std::collections::{HashMap, HashSet};
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
|
||||||
|
enum Orientation {
|
||||||
|
North,
|
||||||
|
East,
|
||||||
|
South,
|
||||||
|
West,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Orientation {
|
||||||
|
fn turn(&mut self, direction: &str) {
|
||||||
|
match direction {
|
||||||
|
"R" => match self {
|
||||||
|
Orientation::North => *self = Self::East,
|
||||||
|
Orientation::East => *self = Self::South,
|
||||||
|
Orientation::South => *self = Self::West,
|
||||||
|
Orientation::West => *self = Self::North,
|
||||||
|
},
|
||||||
|
"L" => match self {
|
||||||
|
Orientation::North => *self = Self::West,
|
||||||
|
Orientation::West => *self = Self::South,
|
||||||
|
Orientation::South => *self = Self::East,
|
||||||
|
Orientation::East => *self = Self::North,
|
||||||
|
},
|
||||||
|
_ => panic!("Wrong direction. Only R and L allowed"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn process_part1(input: &str) -> i32 {
|
||||||
|
let mut orientation = Orientation::North;
|
||||||
|
let mut movements = HashMap::new();
|
||||||
|
let mut input = input.to_string();
|
||||||
|
input.pop();
|
||||||
|
for movement in input.split(", ") {
|
||||||
|
let (direction, blocks) = movement.split_at(1);
|
||||||
|
orientation.turn(direction);
|
||||||
|
let blocks = blocks.parse::<i32>().unwrap();
|
||||||
|
movements
|
||||||
|
.entry(orientation)
|
||||||
|
.and_modify(|distance| *distance += blocks)
|
||||||
|
.or_insert(blocks);
|
||||||
|
}
|
||||||
|
let up = movements.get(&Orientation::North).unwrap_or(&0);
|
||||||
|
let down = movements.get(&Orientation::South).unwrap_or(&0);
|
||||||
|
let left = movements.get(&Orientation::West).unwrap_or(&0);
|
||||||
|
let right = movements.get(&Orientation::East).unwrap_or(&0);
|
||||||
|
(up - down).abs() + (left - right).abs()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn process_part2(input: &str) -> i32 {
|
||||||
|
let mut orientation = Orientation::North;
|
||||||
|
let mut visited_coords = HashSet::new();
|
||||||
|
let mut horizontal: i32 = 0;
|
||||||
|
let mut vertical: i32 = 0;
|
||||||
|
let mut input = input.to_string();
|
||||||
|
input.pop();
|
||||||
|
for movement in input.split(", ") {
|
||||||
|
let (direction, blocks) = movement.split_at(1);
|
||||||
|
orientation.turn(direction);
|
||||||
|
let blocks = blocks.parse::<i32>().unwrap();
|
||||||
|
for _block in 1..=blocks {
|
||||||
|
match orientation {
|
||||||
|
Orientation::North => vertical += 1,
|
||||||
|
Orientation::South => vertical -= 1,
|
||||||
|
Orientation::East => horizontal += 1,
|
||||||
|
Orientation::West => horizontal -= 1,
|
||||||
|
}
|
||||||
|
if visited_coords.contains(&(horizontal, vertical)) {
|
||||||
|
return horizontal.abs() + vertical.abs();
|
||||||
|
} else {
|
||||||
|
visited_coords.insert((horizontal, vertical));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
panic!("Did not find easter bunny hq");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
const INPUT1: &str = "R2, L3 ";
|
||||||
|
const INPUT2: &str = "R2, R2, R2 ";
|
||||||
|
const INPUT3: &str = "R5, L5, R5, R3 ";
|
||||||
|
const INPUT4: &str = "R8, R4, R4, R8 ";
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part1() {
|
||||||
|
let result = process_part1(INPUT1);
|
||||||
|
assert_eq!(result, 5);
|
||||||
|
let result = process_part1(INPUT2);
|
||||||
|
assert_eq!(result, 2);
|
||||||
|
let result = process_part1(INPUT3);
|
||||||
|
assert_eq!(result, 12);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part2() {
|
||||||
|
let result = process_part2(INPUT4);
|
||||||
|
assert_eq!(result, 4);
|
||||||
|
}
|
||||||
|
}
|
1
y2016/src/days/mod.rs
Normal file
1
y2016/src/days/mod.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub mod d1;
|
@ -1 +1 @@
|
|||||||
|
pub mod days;
|
||||||
|
Loading…
Reference in New Issue
Block a user