[TASK] Solved Day 15
This commit is contained in:
1
input/day15.txt
Normal file
1
input/day15.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
17,1,3,16,19,0
|
||||||
1
input/day15_example.txt
Normal file
1
input/day15_example.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
0,3,6
|
||||||
15
src/day14.rs
15
src/day14.rs
@@ -31,12 +31,13 @@ fn solve_part1(lines: &Vec<String>) -> u64 {
|
|||||||
|
|
||||||
fn solve_part2(lines: &Vec<String>) -> u64 {
|
fn solve_part2(lines: &Vec<String>) -> u64 {
|
||||||
|
|
||||||
/// Idea for performance optimalization: use a Vec containing the "root" memory position, and
|
/*
|
||||||
/// the mask of the floats used to write to that position.
|
Idea for performance optimalization: use a Vec containing the "root" memory position, and
|
||||||
/// Whenever something is written, check (using bit operations) if a collision between existing
|
the mask of the floats used to write to that position.
|
||||||
/// memory positions is possible. If that is the case, modify the old (existing) entry by
|
Whenever something is written, check (using bit operations) if a collision between existing
|
||||||
/// removing all overlapping entries in the float mask.
|
memory positions is possible. If that is the case, modify the old (existing) entry by
|
||||||
///
|
removing all overlapping entries in the float mask.
|
||||||
|
*/
|
||||||
|
|
||||||
let mut mem: HashMap<u64, u64> = HashMap::new();
|
let mut mem: HashMap<u64, u64> = HashMap::new();
|
||||||
let mut mask = MemoryBitMask { ones_mask: 0, float_masks: Vec::new() };
|
let mut mask = MemoryBitMask { ones_mask: 0, float_masks: Vec::new() };
|
||||||
@@ -85,9 +86,7 @@ impl BitMask {
|
|||||||
|
|
||||||
|
|
||||||
fn apply(&self, x: u64) -> u64 {
|
fn apply(&self, x: u64) -> u64 {
|
||||||
|
|
||||||
return (x & self.zeros_mask) | self.ones_mask;
|
return (x & self.zeros_mask) | self.ones_mask;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
41
src/day15.rs
Normal file
41
src/day15.rs
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
use super::util;
|
||||||
|
|
||||||
|
pub fn solve() {
|
||||||
|
let lines = util::read_file("input/day15.txt");
|
||||||
|
let starting_numbers = lines[0].split(",").map(|s| s.parse::<usize>().unwrap()).collect::<Vec<_>>();
|
||||||
|
|
||||||
|
let part1 = play_until(2020, &starting_numbers);
|
||||||
|
println!("Day X Part 1: {}", part1);
|
||||||
|
|
||||||
|
|
||||||
|
let part2 = play_until(30_000_000, &starting_numbers);
|
||||||
|
println!("Day X Part 2: {}", part2);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn play_until(max_turn: usize, starting_numbers: &Vec<usize>) -> usize {
|
||||||
|
|
||||||
|
// Because numbers can never be bigger than the number of turns, this works:
|
||||||
|
let mut last_occurrence = vec!{0usize; max_turn};
|
||||||
|
|
||||||
|
// Note: we ignore the last number here; it will be the "previous_number" later.
|
||||||
|
for i in 0..(starting_numbers.len() - 1) {
|
||||||
|
let n = starting_numbers[i];
|
||||||
|
last_occurrence[n] = i + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut previous_number = starting_numbers[starting_numbers.len() - 1];
|
||||||
|
for turn in starting_numbers.len()..max_turn {
|
||||||
|
|
||||||
|
let prev_occurrence = last_occurrence[previous_number];
|
||||||
|
last_occurrence[previous_number] = turn;
|
||||||
|
if prev_occurrence == 0 {
|
||||||
|
previous_number = 0;
|
||||||
|
} else {
|
||||||
|
previous_number = turn - prev_occurrence;
|
||||||
|
}
|
||||||
|
println!("Turn {}: {}", turn + 1, previous_number);
|
||||||
|
}
|
||||||
|
|
||||||
|
return previous_number;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -16,9 +16,10 @@ mod day11;
|
|||||||
mod day12;
|
mod day12;
|
||||||
mod day13;
|
mod day13;
|
||||||
mod day14;
|
mod day14;
|
||||||
|
mod day15;
|
||||||
|
|
||||||
const MAX_DAY: u8 = 14;
|
const MAX_DAY: u8 = 15;
|
||||||
const BENCHMARK_AMOUNT: u32 = 1000;
|
const BENCHMARK_AMOUNT: u32 = 100;
|
||||||
|
|
||||||
fn solve(day: u8) {
|
fn solve(day: u8) {
|
||||||
match day {
|
match day {
|
||||||
@@ -36,6 +37,7 @@ fn solve(day: u8) {
|
|||||||
12 => day12::solve(),
|
12 => day12::solve(),
|
||||||
13 => day13::solve(),
|
13 => day13::solve(),
|
||||||
14 => day14::solve(),
|
14 => day14::solve(),
|
||||||
|
15 => day15::solve(),
|
||||||
_ => println!("This day is not yet implemented")
|
_ => println!("This day is not yet implemented")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user