feat: added day 14 part 1 and 2
All checks were successful
Build and run challenges / Challenge for day (1) (push) Successful in 3s
Build and run challenges / Challenge for day (10) (push) Successful in 3s
Build and run challenges / Challenge for day (11) (push) Successful in 3s
Build and run challenges / Challenge for day (12) (push) Successful in 3s
Build and run challenges / Challenge for day (13) (push) Successful in 3s
Build and run challenges / Challenge for day (14) (push) Successful in 3s
Build and run challenges / Challenge for day (2) (push) Successful in 3s
Build and run challenges / Challenge for day (4) (push) Successful in 5s
Build and run challenges / Challenge for day (5) (push) Successful in 5s
Build and run challenges / Challenge for day (6) (push) Successful in 5s
Build and run challenges / Challenge for day (3) (push) Successful in 11s
Build and run challenges / Challenge for day (7) (push) Successful in 4s
Build and run challenges / Challenge for day (8) (push) Successful in 3s
Build and run challenges / Challenge for day (9) (push) Successful in 3s
All checks were successful
Build and run challenges / Challenge for day (1) (push) Successful in 3s
Build and run challenges / Challenge for day (10) (push) Successful in 3s
Build and run challenges / Challenge for day (11) (push) Successful in 3s
Build and run challenges / Challenge for day (12) (push) Successful in 3s
Build and run challenges / Challenge for day (13) (push) Successful in 3s
Build and run challenges / Challenge for day (14) (push) Successful in 3s
Build and run challenges / Challenge for day (2) (push) Successful in 3s
Build and run challenges / Challenge for day (4) (push) Successful in 5s
Build and run challenges / Challenge for day (5) (push) Successful in 5s
Build and run challenges / Challenge for day (6) (push) Successful in 5s
Build and run challenges / Challenge for day (3) (push) Successful in 11s
Build and run challenges / Challenge for day (7) (push) Successful in 4s
Build and run challenges / Challenge for day (8) (push) Successful in 3s
Build and run challenges / Challenge for day (9) (push) Successful in 3s
Signed-off-by: Louis Vallat <contact@louis-vallat.dev>
This commit is contained in:
parent
7907f2d1b4
commit
a132e3f098
@ -11,7 +11,7 @@ jobs:
|
|||||||
name: Challenge for day
|
name: Challenge for day
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
day_number: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
|
day_number: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
|
||||||
runs-on: rust-bookworm
|
runs-on: rust-bookworm
|
||||||
steps:
|
steps:
|
||||||
- name: Check out repository code
|
- name: Check out repository code
|
||||||
|
6
day14/Cargo.toml
Normal file
6
day14/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "day14"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
121
day14/src/main.rs
Normal file
121
day14/src/main.rs
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
use std::{env, fs};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
struct Robot {
|
||||||
|
position: (i32, i32),
|
||||||
|
direction: (i32, i32),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Robot {
|
||||||
|
fn advance(&mut self, steps: i32, board_height: i32, board_width: i32) {
|
||||||
|
self.position.0 = (self.position.0 + steps * (board_height + self.direction.0)) % board_height;
|
||||||
|
self.position.1 = (self.position.1 + steps * (board_width + self.direction.1)) % board_width;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn read_input(path: &str) -> String {
|
||||||
|
fs::read_to_string(path).expect("Cannot read file.")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_input(input: &str) -> Vec<Robot> {
|
||||||
|
let mut robots = Vec::new();
|
||||||
|
for line in input.lines().filter(|line| !line.is_empty()) {
|
||||||
|
let halves = line.split_once(' ').unwrap();
|
||||||
|
let p = halves.0[2..].split_once(',').unwrap();
|
||||||
|
let v = halves.1[2..].split_once(',').unwrap();
|
||||||
|
robots.push(Robot {
|
||||||
|
position: (p.1.parse().unwrap(), p.0.parse().unwrap()),
|
||||||
|
direction: (v.1.parse().unwrap(), v.0.parse().unwrap()),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
robots
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_trunk(map: &Vec<Vec<u32>>, position: (usize, usize)) -> bool {
|
||||||
|
for i in 0..=6 {
|
||||||
|
for j in 0..=3 {
|
||||||
|
if map[position.0 + i][position.0 + j] == 0 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn place_robots(robots: &Vec<Robot>, height: usize, width: usize) -> Vec<Vec<u32>> {
|
||||||
|
let mut map: Vec<Vec<u32>> = vec![vec![0; width]; height];
|
||||||
|
|
||||||
|
for robot in robots {
|
||||||
|
map[robot.position.0 as usize][robot.position.1 as usize] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
map
|
||||||
|
}
|
||||||
|
|
||||||
|
fn display_map(map: &Vec<Vec<u32>>) {
|
||||||
|
println!("┌{:─<1$}┐", "", map[0].len());
|
||||||
|
for line in map {
|
||||||
|
print!("│");
|
||||||
|
for r in line {
|
||||||
|
print!("{}", if *r == 0 { " " } else { "█"});
|
||||||
|
}
|
||||||
|
println!("│");
|
||||||
|
}
|
||||||
|
println!("└{:─<1$}┘", "", map[0].len());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part_1(robots: &Vec<Robot>) -> i32 {
|
||||||
|
let mut robots = robots.clone();
|
||||||
|
let (height, width) = (103, 101);
|
||||||
|
let mut quadrants = vec![0; 4];
|
||||||
|
for robot in robots.iter_mut() {
|
||||||
|
robot.advance(100, height, width);
|
||||||
|
if robot.position.0 != height / 2 && robot.position.1 != width / 2 {
|
||||||
|
quadrants[(robot.position.0 / (height / 2 + 1) * 2 + robot.position.1 / (width / 2 + 1)) as usize] += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
quadrants.iter().fold(1, |acc, i| acc * i)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part_2(robots: &Vec<Robot>) -> (i32, Vec<Vec<u32>>) {
|
||||||
|
let mut robots = robots.clone();
|
||||||
|
let (height, width) = (103, 101);
|
||||||
|
let mut map = place_robots(&robots, height, width);
|
||||||
|
let mut step = 1;
|
||||||
|
loop {
|
||||||
|
for robot in robots.iter_mut() {
|
||||||
|
if map[robot.position.0 as usize][robot.position.1 as usize] > 0 {
|
||||||
|
map[robot.position.0 as usize][robot.position.1 as usize] -= 1;
|
||||||
|
}
|
||||||
|
robot.advance(1, height as i32, width as i32);
|
||||||
|
map[robot.position.0 as usize][robot.position.1 as usize] += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for i in 0..97 {
|
||||||
|
for j in 0..98 {
|
||||||
|
if is_trunk(&map, (i, j)) {
|
||||||
|
return (step, map);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
step += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let args: Vec<String> = env::args().collect();
|
||||||
|
for arg in args.iter().skip(1) {
|
||||||
|
let input = read_input(&arg);
|
||||||
|
let robots = parse_input(&input);
|
||||||
|
let (part_2, map) = part_2(&robots);
|
||||||
|
println!("[{}]", &arg);
|
||||||
|
println!("\t[Part 1] => Answer is '{}'.", part_1(&robots));
|
||||||
|
println!("\t[Part 2] => Answer is '{}'.", part_2);
|
||||||
|
println!("\t[BONUS] =>");
|
||||||
|
display_map(&map);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user