mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-09-30 07:23:18 -09:00
do day 9
This commit is contained in:
parent
7de62c73bf
commit
6b1de80d54
2000
2022/input/day_9.txt
Normal file
2000
2022/input/day_9.txt
Normal file
File diff suppressed because it is too large
Load Diff
8
2022/input/day_9_sample.txt
Normal file
8
2022/input/day_9_sample.txt
Normal file
@ -0,0 +1,8 @@
|
||||
R 5
|
||||
U 8
|
||||
L 8
|
||||
D 3
|
||||
R 17
|
||||
D 10
|
||||
L 25
|
||||
U 20
|
141
2022/src/day_9.rs
Normal file
141
2022/src/day_9.rs
Normal file
@ -0,0 +1,141 @@
|
||||
use std::collections::HashSet;
|
||||
use Direction::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Direction {
|
||||
L,
|
||||
R,
|
||||
U,
|
||||
D,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Instruction {
|
||||
direction: Direction,
|
||||
steps: i32,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
|
||||
struct Location {
|
||||
x: i32,
|
||||
y: i32,
|
||||
}
|
||||
|
||||
type Data = Vec<Instruction>;
|
||||
type Output = usize;
|
||||
fn parse(input: &str) -> Data {
|
||||
input
|
||||
.lines()
|
||||
.map(|line| {
|
||||
let (direction, steps) = line.split_once(' ').unwrap();
|
||||
Instruction {
|
||||
direction: match direction {
|
||||
"L" => L,
|
||||
"R" => R,
|
||||
"D" => D,
|
||||
"U" => U,
|
||||
_ => panic!("expected L, R, D, or U. got {}", direction),
|
||||
},
|
||||
steps: steps.parse().unwrap(),
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
fn part_one(data: Data) -> Output {
|
||||
let mut locations = HashSet::<Location>::new();
|
||||
let mut head = Location { x: 0, y: 0 };
|
||||
let mut tail = head;
|
||||
locations.insert(tail);
|
||||
for instr in data {
|
||||
for _ in 0..instr.steps {
|
||||
// println!("{:?}", tail);
|
||||
match instr.direction {
|
||||
L => {
|
||||
head.x -= 1;
|
||||
if tail.x == head.x + 2 {
|
||||
tail.x = head.x + 1;
|
||||
tail.y = head.y;
|
||||
}
|
||||
}
|
||||
R => {
|
||||
head.x += 1;
|
||||
if tail.x == head.x - 2 {
|
||||
tail.x = head.x - 1;
|
||||
tail.y = head.y;
|
||||
}
|
||||
}
|
||||
U => {
|
||||
head.y += 1;
|
||||
if tail.y + 2 == head.y {
|
||||
tail.x = head.x;
|
||||
tail.y = head.y - 1;
|
||||
}
|
||||
}
|
||||
D => {
|
||||
head.y -= 1;
|
||||
if tail.y == head.y + 2 {
|
||||
tail.x = head.x;
|
||||
tail.y = head.y + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
locations.insert(tail);
|
||||
}
|
||||
}
|
||||
locations.len()
|
||||
}
|
||||
fn part_two(data: Data) -> Output {
|
||||
let mut locations = HashSet::<Location>::new();
|
||||
let mut rope = [Location { x: 0, y: 0 }; 10];
|
||||
locations.insert(rope[9]);
|
||||
for instr in data {
|
||||
for _ in 0..instr.steps {
|
||||
match instr.direction {
|
||||
L => {
|
||||
rope[0].x -= 1;
|
||||
}
|
||||
R => {
|
||||
rope[0].x += 1;
|
||||
}
|
||||
U => {
|
||||
rope[0].y += 1;
|
||||
}
|
||||
D => {
|
||||
rope[0].y -= 1;
|
||||
}
|
||||
}
|
||||
for i in 1..rope.len() {
|
||||
if rope[i].x.abs_diff(rope[i - 1].x) == 2 && rope[i].y.abs_diff(rope[i - 1].y) == 2
|
||||
{
|
||||
rope[i] = Location {
|
||||
x: rope[i - 1].x + if rope[i - 1].x > rope[i].x { -1 } else { 1 },
|
||||
y: rope[i - 1].y + if rope[i - 1].y > rope[i].y { -1 } else { 1 },
|
||||
};
|
||||
} else if rope[i].x >= rope[i - 1].x + 2 {
|
||||
rope[i].x = rope[i - 1].x + 1;
|
||||
rope[i].y = rope[i - 1].y;
|
||||
} else if rope[i].y >= rope[i - 1].y + 2 {
|
||||
rope[i].x = rope[i - 1].x;
|
||||
rope[i].y = rope[i - 1].y + 1;
|
||||
} else if rope[i].y <= rope[i - 1].y - 2 {
|
||||
rope[i].x = rope[i - 1].x;
|
||||
rope[i].y = rope[i - 1].y - 1;
|
||||
} else if rope[i].x <= rope[i - 1].x - 2 {
|
||||
rope[i].x = rope[i - 1].x - 1;
|
||||
rope[i].y = rope[i - 1].y;
|
||||
}
|
||||
}
|
||||
locations.insert(rope[rope.len() - 1]);
|
||||
}
|
||||
}
|
||||
locations.len()
|
||||
}
|
||||
|
||||
advent_of_code_macro::generate_tests!(
|
||||
day 9,
|
||||
parse,
|
||||
part_one,
|
||||
part_two,
|
||||
sample tests [88, 36],
|
||||
star tests [6337, 2455]
|
||||
);
|
@ -7,3 +7,4 @@ mod day_5;
|
||||
mod day_6;
|
||||
mod day_7;
|
||||
mod day_8;
|
||||
mod day_9;
|
||||
|
Loading…
x
Reference in New Issue
Block a user