mirror of
https://github.com/wezm/advent-of-code.git
synced 2024-12-18 18:29:55 +00:00
Day 7 part 1
This commit is contained in:
parent
1f34121f0a
commit
21c50bd8e8
5 changed files with 111 additions and 11 deletions
1
2019/input/day7.txt
Normal file
1
2019/input/day7.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
3,8,1001,8,10,8,105,1,0,0,21,42,55,64,77,94,175,256,337,418,99999,3,9,102,4,9,9,1001,9,5,9,102,2,9,9,101,3,9,9,4,9,99,3,9,102,2,9,9,101,5,9,9,4,9,99,3,9,1002,9,4,9,4,9,99,3,9,102,4,9,9,101,5,9,9,4,9,99,3,9,102,5,9,9,1001,9,3,9,1002,9,5,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,99
|
|
@ -1,3 +1,4 @@
|
||||||
|
use advent_of_code::number_to_digits;
|
||||||
use std::ops::RangeInclusive;
|
use std::ops::RangeInclusive;
|
||||||
|
|
||||||
const INPUT: RangeInclusive<u32> = 356261..=846303;
|
const INPUT: RangeInclusive<u32> = 356261..=846303;
|
||||||
|
@ -43,16 +44,6 @@ fn meets_criteria_part2(number: &u32) -> bool {
|
||||||
chunks.iter().any(|chunk| chunk.len() == 2) && digits.windows(2).all(|pair| pair[1] >= pair[0])
|
chunks.iter().any(|chunk| chunk.len() == 2) && digits.windows(2).all(|pair| pair[1] >= pair[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
fn number_to_digits(mut number: u32) -> [u8; 6] {
|
|
||||||
let mut digits = [0; 6];
|
|
||||||
digits.iter_mut().rev().for_each(|digit| {
|
|
||||||
*digit = (number % 10) as u8;
|
|
||||||
number /= 10;
|
|
||||||
});
|
|
||||||
|
|
||||||
digits
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
76
2019/src/bin/day7.rs
Normal file
76
2019/src/bin/day7.rs
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
use std::{fs, io};
|
||||||
|
|
||||||
|
use advent_of_code::{computer, input};
|
||||||
|
|
||||||
|
const AMPLIFIERS: usize = 5;
|
||||||
|
|
||||||
|
fn main() -> io::Result<()> {
|
||||||
|
let source = fs::read_to_string("input/day7.txt")?;
|
||||||
|
let data = input::read_separated_line(',', &source)?;
|
||||||
|
|
||||||
|
let max = phase_settings()
|
||||||
|
.iter()
|
||||||
|
.map(|settings| {
|
||||||
|
let mut output = 0;
|
||||||
|
for phase_setting in settings.iter() {
|
||||||
|
let input = vec![output, *phase_setting];
|
||||||
|
let mut program = data.clone();
|
||||||
|
let mut computer = computer::Computer::new(&mut program, input);
|
||||||
|
computer.run(None, None);
|
||||||
|
output = computer.output()[0];
|
||||||
|
}
|
||||||
|
output
|
||||||
|
})
|
||||||
|
.max()
|
||||||
|
.unwrap();
|
||||||
|
println!("Part 1: {}", max);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn phase_settings() -> Vec<[i32; AMPLIFIERS]> {
|
||||||
|
let mut permutations = Vec::new();
|
||||||
|
let mut indexes = [0i32; AMPLIFIERS];
|
||||||
|
let mut elements = [0i32; AMPLIFIERS];
|
||||||
|
for i in 0i32..=4 {
|
||||||
|
elements[i as usize] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
permutations.push(elements);
|
||||||
|
|
||||||
|
let mut i = 0;
|
||||||
|
while i < AMPLIFIERS {
|
||||||
|
if indexes[i] < i as i32 {
|
||||||
|
elements.swap(if i % 2 == 0 { 0 } else { indexes[i] as usize }, i);
|
||||||
|
permutations.push(elements);
|
||||||
|
indexes[i] += 1;
|
||||||
|
i = 0;
|
||||||
|
} else {
|
||||||
|
indexes[i] = 0;
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
permutations
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_phase_setting_generation() {
|
||||||
|
let settings = phase_settings();
|
||||||
|
assert_eq!(
|
||||||
|
&settings[0..6],
|
||||||
|
&[
|
||||||
|
[0, 1, 2, 3, 4],
|
||||||
|
[1, 0, 2, 3, 4],
|
||||||
|
[2, 0, 1, 3, 4],
|
||||||
|
[0, 2, 1, 3, 4],
|
||||||
|
[1, 2, 0, 3, 4],
|
||||||
|
[2, 1, 0, 3, 4]
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -188,7 +188,7 @@ mod tests {
|
||||||
fn test_day2() {
|
fn test_day2() {
|
||||||
let input = fs::read_to_string("input/day2.txt").unwrap();
|
let input = fs::read_to_string("input/day2.txt").unwrap();
|
||||||
let mut data = input::read_separated_line(',', &input).unwrap();
|
let mut data = input::read_separated_line(',', &input).unwrap();
|
||||||
let mut program = Computer::new(&mut data);
|
let mut program = Computer::new(&mut data, vec![]);
|
||||||
|
|
||||||
// Check that day2 still works wirh run through this implementation
|
// Check that day2 still works wirh run through this implementation
|
||||||
program.run(Some(12), Some(2));
|
program.run(Some(12), Some(2));
|
||||||
|
|
|
@ -1,3 +1,35 @@
|
||||||
pub mod computer;
|
pub mod computer;
|
||||||
pub mod input;
|
pub mod input;
|
||||||
pub mod point;
|
pub mod point;
|
||||||
|
|
||||||
|
pub fn number_to_digits(mut number: u32) -> [u8; 6] {
|
||||||
|
let mut digits = [0; 6];
|
||||||
|
digits.iter_mut().rev().for_each(|digit| {
|
||||||
|
*digit = (number % 10) as u8;
|
||||||
|
number /= 10;
|
||||||
|
});
|
||||||
|
|
||||||
|
digits
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn quinary(upto: u32) -> Vec<[u8; 5]> {
|
||||||
|
let limit = number_to_digits(upto);
|
||||||
|
let mut result = Vec::new();
|
||||||
|
|
||||||
|
let mut counter = [0; 5];
|
||||||
|
while &counter != &limit[1..] {
|
||||||
|
result.push(counter);
|
||||||
|
counter[0] += 1;
|
||||||
|
for i in 0..5 {
|
||||||
|
if counter[i] == 5 {
|
||||||
|
counter[i] = 0;
|
||||||
|
counter[i + 1] += 1;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result.push(counter);
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue