Compare commits
2 commits
d63a8d37e5
...
a87505aa6d
Author | SHA1 | Date | |
---|---|---|---|
|
a87505aa6d | ||
|
168479c5b1 |
4 changed files with 267 additions and 59 deletions
143
res/10/input.txt
Normal file
143
res/10/input.txt
Normal file
|
@ -0,0 +1,143 @@
|
|||
addx 2
|
||||
addx 3
|
||||
noop
|
||||
noop
|
||||
addx 1
|
||||
addx 5
|
||||
addx -1
|
||||
addx 5
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
addx 4
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
addx -5
|
||||
addx 6
|
||||
addx 3
|
||||
addx 1
|
||||
addx 5
|
||||
addx 1
|
||||
noop
|
||||
addx -38
|
||||
addx 41
|
||||
addx -22
|
||||
addx -14
|
||||
addx 7
|
||||
noop
|
||||
noop
|
||||
addx 3
|
||||
addx -2
|
||||
addx 2
|
||||
noop
|
||||
addx 17
|
||||
addx -12
|
||||
addx 5
|
||||
addx 2
|
||||
addx -16
|
||||
addx 17
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
addx -30
|
||||
noop
|
||||
addx -6
|
||||
addx 1
|
||||
noop
|
||||
addx 5
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
addx -12
|
||||
addx 17
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
addx 10
|
||||
addx -9
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
addx -5
|
||||
addx 6
|
||||
addx 4
|
||||
noop
|
||||
noop
|
||||
addx -37
|
||||
noop
|
||||
noop
|
||||
addx 17
|
||||
addx -12
|
||||
addx 30
|
||||
addx -23
|
||||
addx 2
|
||||
noop
|
||||
addx 3
|
||||
addx -17
|
||||
addx 22
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
noop
|
||||
addx -10
|
||||
addx 11
|
||||
addx 4
|
||||
noop
|
||||
addx 5
|
||||
addx -2
|
||||
noop
|
||||
addx -6
|
||||
addx -29
|
||||
addx 37
|
||||
addx -30
|
||||
addx 27
|
||||
addx -2
|
||||
addx -22
|
||||
noop
|
||||
addx 3
|
||||
addx 2
|
||||
noop
|
||||
addx 7
|
||||
addx -2
|
||||
addx 2
|
||||
addx 5
|
||||
addx -5
|
||||
addx 6
|
||||
addx 2
|
||||
addx 2
|
||||
addx 5
|
||||
addx -25
|
||||
noop
|
||||
addx -10
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
addx 2
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 7
|
||||
addx 1
|
||||
addx 4
|
||||
addx 1
|
||||
noop
|
||||
addx 2
|
||||
noop
|
||||
addx 3
|
||||
addx 5
|
||||
addx -1
|
||||
noop
|
||||
addx 3
|
||||
addx 5
|
||||
addx 2
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
|
@ -1,9 +1,9 @@
|
|||
use std::fs;
|
||||
|
||||
const GRID_WIDTH: u32 = 1000;
|
||||
const GRID_HEIGHT: u32 = 1000;
|
||||
const START_X: u32 = 500;
|
||||
const START_Y: u32 = 500;
|
||||
const GRID_WIDTH: i32 = 1000;
|
||||
const GRID_HEIGHT: i32 = 1000;
|
||||
const START_X: i32 = 500;
|
||||
const START_Y: i32 = 500;
|
||||
|
||||
|
||||
pub fn solve() {
|
||||
|
@ -32,7 +32,7 @@ pub fn solve() {
|
|||
"R" => Dir::Right,
|
||||
_ => Dir::Up
|
||||
};
|
||||
let amount = cmd[1].parse::<u32>().unwrap();
|
||||
let amount = cmd[1].parse::<i32>().unwrap();
|
||||
|
||||
// move head
|
||||
for _ in 0..amount {
|
||||
|
@ -62,13 +62,13 @@ enum Dir {
|
|||
}
|
||||
|
||||
struct Rope {
|
||||
knots_x: Vec<u32>,
|
||||
knots_y: Vec<u32>,
|
||||
knots_x: Vec<i32>,
|
||||
knots_y: Vec<i32>,
|
||||
tail_visited_grid: Vec<Vec<bool>> // [Y][X]
|
||||
}
|
||||
|
||||
impl Rope {
|
||||
pub fn new(knots: u32) -> Self {
|
||||
pub fn new(knots: i32) -> Self {
|
||||
Rope {
|
||||
knots_x: (0..knots).map(|_| START_X).collect(),
|
||||
knots_y: (0..knots).map(|_| START_Y).collect(),
|
||||
|
@ -102,67 +102,51 @@ impl Rope {
|
|||
fn check_tail_pos(&mut self) {
|
||||
// check pos of each knot except first
|
||||
for i in 1..self.knots_x.len() {
|
||||
// head is too far top/bottom
|
||||
if self.knots_x[i-1].abs_diff(self.knots_x[i]) > 1 {
|
||||
// if necessary, move on Y-axis
|
||||
self.knots_y[i] = self.knots_y[i-1];
|
||||
|
||||
if self.knots_x[i-1] > self.knots_x[i] { // head below
|
||||
self.knots_x[i] = self.knots_x[i-1]-1;
|
||||
} else { // head above
|
||||
self.knots_x[i] = self.knots_x[i-1]+1;
|
||||
}
|
||||
}
|
||||
|
||||
// head is too far left/right
|
||||
else if self.knots_y[i-1].abs_diff(self.knots_y[i]) > 1 {
|
||||
// if necessary, move on X-axis
|
||||
self.knots_x[i] = self.knots_x[i-1];
|
||||
|
||||
if self.knots_y[i-1] > self.knots_y[i] { // head right
|
||||
self.knots_y[i] = self.knots_y[i-1]-1;
|
||||
} else { // head left
|
||||
self.knots_y[i] = self.knots_y[i-1]+1;
|
||||
if (self.knots_x[i-1] == self.knots_x[i] && (self.knots_y[i-1] - self.knots_y[i]).abs() == 1)
|
||||
|| (self.knots_y[i-1] == self.knots_y[i] && (self.knots_x[i-1] - self.knots_x[i]).abs() == 1) {
|
||||
// Knots are touching, nothing to do
|
||||
} else if (self.knots_x[i-1] - self.knots_x[i]).abs() == 1 && (self.knots_y[i-1] - self.knots_y[i]).abs() == 1 {
|
||||
// Knots are touching diagonally, nothing to do
|
||||
} else {
|
||||
// Tail needs to be moved
|
||||
if self.knots_x[i-1] != self.knots_x[i] && self.knots_y[i-1] != self.knots_y[i] {
|
||||
// Move diagonally
|
||||
self.knots_x[i] += normalize(self.knots_x[i-1] - self.knots_x[i]);
|
||||
self.knots_y[i] += normalize(self.knots_y[i-1] - self.knots_y[i]);
|
||||
} else {
|
||||
// Move vertically/horizontally
|
||||
if self.knots_x[i-1] == self.knots_x[i] {
|
||||
self.knots_y[i] += normalize(self.knots_y[i-1] - self.knots_y[i]);
|
||||
} else {
|
||||
self.knots_x[i] += normalize(self.knots_x[i-1] - self.knots_x[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// set current tail (last knot) pos as visited
|
||||
// Set current tail (last knot) pos as visited
|
||||
self.tail_visited_grid[*self.knots_y.last().unwrap() as usize][*self.knots_x.last().unwrap() as usize] = true;
|
||||
|
||||
/*
|
||||
let min_x = self.knots_x.iter().min().unwrap();
|
||||
let max_x = self.knots_x.iter().max().unwrap();
|
||||
let min_y = self.knots_y.iter().min().unwrap();
|
||||
let max_y = self.knots_y.iter().max().unwrap();
|
||||
|
||||
let width = max_x - min_x + 1;
|
||||
let height = max_y - min_y + 1;
|
||||
|
||||
let mut out: Vec<Vec<String>> = (0..height).map(|_| (0..width).map(|_| ".".to_string()).collect()).collect();
|
||||
for i in (0..self.knots_x.len()).rev() {
|
||||
let x = self.knots_x[i]-min_x;
|
||||
let y = self.knots_y[i]-min_y;
|
||||
out[y as usize][x as usize] = i.to_string();
|
||||
}
|
||||
println!("{}", min_y);
|
||||
out.iter().for_each(|line| {
|
||||
line.iter().for_each(|o| print!("{}",o));
|
||||
println!();
|
||||
});
|
||||
println!();*/
|
||||
|
||||
}
|
||||
|
||||
// returns number of cells visited by tail
|
||||
pub fn tail_visited_num(&self) -> u32 {
|
||||
pub fn tail_visited_num(&self) -> i32 {
|
||||
self.tail_visited_grid.iter()
|
||||
.map(|row|
|
||||
row.iter()
|
||||
.map(|cell|
|
||||
if *cell { 1 as u32 }
|
||||
else { 0 as u32 }
|
||||
).sum::<u32>()
|
||||
if *cell { 1 as i32 }
|
||||
else { 0 as i32 }
|
||||
).sum::<i32>()
|
||||
).sum()
|
||||
}
|
||||
}
|
||||
|
||||
fn normalize(n: i32) -> i32 {
|
||||
if n > 0 {
|
||||
1
|
||||
} else if n < 0 {
|
||||
-1
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
79
src/days/d10.rs
Normal file
79
src/days/d10.rs
Normal file
|
@ -0,0 +1,79 @@
|
|||
use std::fs;
|
||||
|
||||
pub fn solve() {
|
||||
let path = "res/10/input.txt";
|
||||
|
||||
let contents = fs::read_to_string(path).expect("I/O error, wrong path?");
|
||||
|
||||
let lines: Vec<&str> = contents
|
||||
.lines()
|
||||
.filter(|line| *line != "")
|
||||
.collect();
|
||||
|
||||
let mut reg_x = 1;
|
||||
let mut cycle = 0;
|
||||
let mut signal_strengths = Vec::<i32>::new();
|
||||
|
||||
let mut crt = Vec::<bool>::new();
|
||||
|
||||
// parse commands line by line
|
||||
lines.iter().for_each(|line| {
|
||||
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||
match parts[0] {
|
||||
"noop" => {
|
||||
// increase cycle 1 time
|
||||
cycle += 1;
|
||||
if rel_cycle(cycle) {
|
||||
signal_strengths.push(cycle*reg_x);
|
||||
}
|
||||
crt.push(sprite_draw(cycle, reg_x));
|
||||
},
|
||||
"addx" => {
|
||||
// increase cycle 2 times
|
||||
(0..2).for_each(|_| {
|
||||
cycle += 1;
|
||||
if rel_cycle(cycle) {
|
||||
signal_strengths.push(cycle*reg_x);
|
||||
}
|
||||
crt.push(sprite_draw(cycle, reg_x));
|
||||
});
|
||||
// change regX according to parameter
|
||||
reg_x += parts[1].parse::<i32>().unwrap();
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
});
|
||||
|
||||
let res: i32 = signal_strengths.iter().sum();
|
||||
|
||||
println!("Result 1: {}", res);
|
||||
|
||||
println!("Result 2: ");
|
||||
let mut print_buffer = "".to_string();
|
||||
// print the crt line by line
|
||||
crt.iter().enumerate().for_each(|(i, p)| {
|
||||
if *p { print_buffer += "#" }
|
||||
else { print_buffer += "." }
|
||||
|
||||
// print buffer if a line is completed
|
||||
if (i+1) % 40 == 0 {
|
||||
println!("{}", print_buffer);
|
||||
print_buffer = "".to_string();
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// returns whether the passed cycle is a relevant one
|
||||
fn rel_cycle(cycle: i32) -> bool {
|
||||
if (cycle-20) % 40 == 0 && cycle >= 20 {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
// returns whether the sprite is currently in a position to draw a pixel
|
||||
fn sprite_draw(cycle: i32, reg_x: i32) -> bool {
|
||||
return ((cycle-1)%40).abs_diff(reg_x) <= 1
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
pub mod days;
|
||||
|
||||
fn main() {
|
||||
days::d09::solve()
|
||||
days::d10::solve()
|
||||
}
|
||||
|
||||
fn _all_days() {
|
||||
|
@ -23,4 +23,6 @@ fn _all_days() {
|
|||
days::d08::solve();
|
||||
println!("\nDay 9");
|
||||
days::d09::solve();
|
||||
println!("\nDay 10");
|
||||
days::d10::solve();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue