aoc2022 day09 part one

This commit is contained in:
Max Hohlfeld 2022-12-09 11:42:16 +01:00
parent f6ad00ad27
commit 0a9b91e0f3
4 changed files with 2173 additions and 0 deletions

2001
src/aoc2022/day09/input.txt Normal file

File diff suppressed because it is too large Load Diff

168
src/aoc2022/day09/mod.rs Normal file
View File

@ -0,0 +1,168 @@
use std::collections::HashMap;
fn move_tail(head: &(isize, isize), mut tail: (isize, isize)) -> (isize, isize) {
if head.0 == tail.0 && head.1 == tail.1 {
// same spot, wich is fine
} else if head.1 == tail.1 {
// x coords are different -> horizontal movement
if tail.0 + 2 == head.0 {
tail.0 = tail.0 + 1;
} else if tail.0 - 2 == head.0 {
tail.0 = tail.0 - 1;
}
} else if head.0 == tail.0 {
// y coords are different -> vertical movement
if tail.1 + 2 == head.1 {
tail.1 = tail.1 + 1;
} else if tail.1 - 2 == head.1 {
tail.1 = tail.1 - 1;
}
} else {
// both coords are different -> horizontal and vertical movement
if head.0 == tail.0 + 1 {
if head.1 == tail.1 + 2 {
tail.0 = tail.0 + 1;
tail.1 = tail.1 + 1;
} else if head.1 == tail.1 - 2 {
tail.0 = tail.0 + 1;
tail.1 = tail.1 - 1;
}
} else if head.0 == tail.0 - 1 {
if head.1 == tail.1 + 2 {
tail.0 = tail.0 - 1;
tail.1 = tail.1 + 1;
} else if head.1 == tail.1 - 2 {
tail.0 = tail.0 - 1;
tail.1 = tail.1 - 1;
}
} else if head.0 == tail.0 + 2 {
if head.1 == tail.1 + 1 {
tail.0 = tail.0 + 1;
tail.1 = tail.1 + 1;
} else if head.1 == tail.1 - 1 {
tail.0 = tail.0 + 1;
tail.1 = tail.1 - 1;
}
} else if head.0 == tail.0 - 2 {
if head.1 == tail.1 + 1 {
tail.0 = tail.0 - 1;
tail.1 = tail.1 + 1;
} else if head.1 == tail.1 - 1 {
tail.0 = tail.0 - 1;
tail.1 = tail.1 - 1;
}
}
}
tail
}
pub fn task_one(input: &str) -> String {
let mut visited_places: HashMap<(isize, isize), u8> = HashMap::new();
let mut head = (0, 0);
let mut tail = (0, 0);
visited_places.insert(tail, 0);
for line in input.trim().lines() {
match line.split_once(" ").unwrap() {
("U", count) => {
for _ in 1..=count.parse::<usize>().unwrap() {
head.1 = head.1 + 1;
tail = move_tail(&head, tail);
visited_places.insert(tail, 0);
}
}
("R", count) => {
for _ in 1..=count.parse::<usize>().unwrap() {
head.0 = head.0 + 1;
tail = move_tail(&head, tail);
visited_places.insert(tail, 0);
}
}
("D", count) => {
for _ in 1..=count.parse::<usize>().unwrap() {
head.1 = head.1 - 1;
tail = move_tail(&head, tail);
visited_places.insert(tail, 0);
}
}
("L", count) => {
for _ in 1..=count.parse::<usize>().unwrap() {
head.0 = head.0 - 1;
tail = move_tail(&head, tail);
visited_places.insert(tail, 0);
}
}
(_, _) => panic!(),
}
}
visited_places.len().to_string()
}
pub fn task_two(input: &str) -> String {
let mut visited_places: HashMap<(isize, isize), u8> = HashMap::new();
let mut knots: [(isize, isize); 10] = [(0, 0); 10];
visited_places.insert(knots[9], 0);
for line in input.trim().lines() {
match line.split_once(" ").unwrap() {
("U", count) => {
for _ in 1..=count.parse::<usize>().unwrap() {
knots[0].1 = knots[0].1 + 1;
for i in 1..10 {
knots[i] = move_tail(&knots[i - 1], knots[i]);
}
visited_places.insert(knots[9], 0);
}
}
("R", count) => {
for _ in 1..=count.parse::<usize>().unwrap() {
knots[0].0 = knots[0].0 + 1;
for i in 1..10 {
knots[i] = move_tail(&knots[i - 1], knots[i]);
}
visited_places.insert(knots[9], 0);
}
}
("D", count) => {
for _ in 1..=count.parse::<usize>().unwrap() {
knots[0].1 = knots[0].1 - 1;
for i in 1..10 {
knots[i] = move_tail(&knots[i - 1], knots[i]);
}
visited_places.insert(knots[9], 0);
}
}
("L", count) => {
for _ in 1..=count.parse::<usize>().unwrap() {
knots[0].0 = knots[0].0 - 1;
for i in 1..10 {
knots[i] = move_tail(&knots[i - 1], knots[i]);
}
visited_places.insert(knots[9], 0);
}
}
(_, _) => panic!(),
}
}
visited_places.len().to_string()
}

View File

@ -6,3 +6,4 @@ pub mod day05;
pub mod day06; pub mod day06;
pub mod day07; pub mod day07;
pub mod day08; pub mod day08;
pub mod day09;

View File

@ -60,4 +60,7 @@ fn main() {
puzzle = Puzzle { day: 8, year: 2022, task_one: aoc2022::day08::task_one, task_two: aoc2022::day08::task_two }; puzzle = Puzzle { day: 8, year: 2022, task_one: aoc2022::day08::task_one, task_two: aoc2022::day08::task_two };
puzzle.solve_and_print(); puzzle.solve_and_print();
puzzle = Puzzle { day: 9, year: 2022, task_one: aoc2022::day09::task_one, task_two: aoc2022::day09::task_two };
puzzle.solve_and_print();
} }