Day 6/7
This commit is contained in:
@@ -13,7 +13,7 @@ fn parse_turn(s: &str) -> i64 {
|
||||
sign * num.parse::<i64>().unwrap()
|
||||
}
|
||||
|
||||
fn main() -> Result<(), io::Error>{
|
||||
fn main() -> Result<(), io::Error> {
|
||||
let f = File::open("1-input.txt")?;
|
||||
let br = BufReader::new(f);
|
||||
let mut position = 50;
|
||||
@@ -31,4 +31,4 @@ fn main() -> Result<(), io::Error>{
|
||||
}
|
||||
println!("Result: {}", zeroes);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
use std::{fs::File, io::{self, BufRead, BufReader}};
|
||||
use std::{
|
||||
fs::File,
|
||||
io::{self, BufRead, BufReader},
|
||||
};
|
||||
|
||||
fn parse_range(s: &str) -> (i64, i64) {
|
||||
let (low_s, high_s) = s.split_once('-').unwrap();
|
||||
@@ -19,7 +22,7 @@ fn invalid_in_range(low: i64, high: i64) -> i64 {
|
||||
if mut_i == 0 {
|
||||
// println!("{} in {}-{}", i, low, high);
|
||||
total += i;
|
||||
break
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,7 +32,8 @@ fn invalid_in_range(low: i64, high: i64) -> i64 {
|
||||
fn main() -> Result<(), io::Error> {
|
||||
let f = File::open("2-input.txt")?;
|
||||
let reader = BufReader::new(f);
|
||||
let mut ranges = reader.lines()
|
||||
let mut ranges = reader
|
||||
.lines()
|
||||
.next()
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
@@ -42,11 +46,11 @@ fn main() -> Result<(), io::Error> {
|
||||
let mut last: i64 = 0;
|
||||
for (low, high) in ranges {
|
||||
let low = low.max(last);
|
||||
if low <= high {
|
||||
if low <= high {
|
||||
total += invalid_in_range(low, high);
|
||||
}
|
||||
last = high + 1;
|
||||
}
|
||||
println!("Result {}", total);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,26 +1,24 @@
|
||||
use std::{fs::File, io::{self, BufRead, BufReader}};
|
||||
use std::{
|
||||
fs::File,
|
||||
io::{self, BufRead, BufReader},
|
||||
};
|
||||
|
||||
fn max_joltage(s: &str) -> u64 {
|
||||
let values = s.chars()
|
||||
let values = s
|
||||
.chars()
|
||||
.map(|x| x.to_digit(10).unwrap())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let max_idx = |xs: &[u32]| {
|
||||
xs.iter()
|
||||
.enumerate()
|
||||
.fold(0, |max, (i, &x)| {
|
||||
if x > xs[max] { i } else { max }
|
||||
})
|
||||
.fold(0, |max, (i, &x)| if x > xs[max] { i } else { max })
|
||||
};
|
||||
|
||||
let (_, total) =
|
||||
(0..12).rev().fold(
|
||||
(0, 0u64),
|
||||
|(start_idx, total), i| {
|
||||
let next_idx = start_idx + max_idx(&values[start_idx..values.len() - i]);
|
||||
(next_idx + 1, 10 * total + values[next_idx] as u64)
|
||||
}
|
||||
);
|
||||
let (_, total) = (0..12).rev().fold((0, 0u64), |(start_idx, total), i| {
|
||||
let next_idx = start_idx + max_idx(&values[start_idx..values.len() - i]);
|
||||
(next_idx + 1, 10 * total + values[next_idx] as u64)
|
||||
});
|
||||
total
|
||||
}
|
||||
|
||||
@@ -34,4 +32,4 @@ fn main() -> Result<(), io::Error> {
|
||||
}
|
||||
println!("Result: {}", total);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,22 @@
|
||||
use std::{fs::File, io::{self, BufRead, BufReader}};
|
||||
use std::{
|
||||
fs::File,
|
||||
io::{self, BufRead, BufReader},
|
||||
};
|
||||
|
||||
const NEIGHBOURS: [(i32, i32); 8] = [
|
||||
(-1, -1), (0, -1), (1, -1),
|
||||
(-1, 0), (1, 0),
|
||||
(-1, 1), (0, 1), (1, 1),
|
||||
(-1, -1),
|
||||
(0, -1),
|
||||
(1, -1),
|
||||
(-1, 0),
|
||||
(1, 0),
|
||||
(-1, 1),
|
||||
(0, 1),
|
||||
(1, 1),
|
||||
];
|
||||
|
||||
fn get_grid_cell(grid: &[Vec<bool>], x: i32, y: i32) -> bool {
|
||||
let (Ok(ux), Ok(uy)) = (usize::try_from(x), usize::try_from(y)) else {
|
||||
return false
|
||||
return false;
|
||||
};
|
||||
grid.get(uy)
|
||||
.and_then(|row| row.get(ux))
|
||||
@@ -18,22 +26,26 @@ fn get_grid_cell(grid: &[Vec<bool>], x: i32, y: i32) -> bool {
|
||||
|
||||
fn count_surrounding(grid: &[Vec<bool>], x: i32, y: i32) -> i32 {
|
||||
NEIGHBOURS
|
||||
.iter()
|
||||
.map(|(dx, dy)| get_grid_cell(grid, x + dx, y + dy))
|
||||
.filter(|&p| p)
|
||||
.count() as i32
|
||||
.iter()
|
||||
.map(|(dx, dy)| get_grid_cell(grid, x + dx, y + dy))
|
||||
.filter(|&p| p)
|
||||
.count() as i32
|
||||
}
|
||||
|
||||
fn update_grid(grid: &[Vec<bool>]) -> Vec<Vec<bool>> {
|
||||
let height = grid.len();
|
||||
let width = grid[0].len();
|
||||
(0..height).map(|y| {
|
||||
(0..width).map(|x| {
|
||||
let x = x as i32;
|
||||
let y = y as i32;
|
||||
get_grid_cell(grid, x, y) && (count_surrounding(grid, x, y) >= 4)
|
||||
}).collect::<Vec<_>>()
|
||||
}).collect::<Vec<_>>()
|
||||
(0..height)
|
||||
.map(|y| {
|
||||
(0..width)
|
||||
.map(|x| {
|
||||
let x = x as i32;
|
||||
let y = y as i32;
|
||||
get_grid_cell(grid, x, y) && (count_surrounding(grid, x, y) >= 4)
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
|
||||
fn count_all_true(grid: &[Vec<bool>]) -> usize {
|
||||
@@ -59,6 +71,9 @@ fn main() -> Result<(), io::Error> {
|
||||
last_grid = next_grid;
|
||||
}
|
||||
|
||||
println!("Result: {}", count_all_true(&original_grid) - count_all_true(&last_grid));
|
||||
println!(
|
||||
"Result: {}",
|
||||
count_all_true(&original_grid) - count_all_true(&last_grid)
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
use std::{cmp::max, fs::File, io::{self, BufRead, BufReader}};
|
||||
use std::{
|
||||
cmp::max,
|
||||
fs::File,
|
||||
io::{self, BufRead, BufReader},
|
||||
};
|
||||
|
||||
fn parse_range(s: &str) -> (u64, u64) {
|
||||
let (s1, s2) = s.split_once('-').unwrap();
|
||||
@@ -17,9 +21,7 @@ fn main() -> Result<(), io::Error> {
|
||||
.collect();
|
||||
ranges.sort_unstable();
|
||||
|
||||
let mut ids: Vec<u64> = lines
|
||||
.map(|l| l.parse::<u64>().unwrap())
|
||||
.collect();
|
||||
let mut ids: Vec<u64> = lines.map(|l| l.parse::<u64>().unwrap()).collect();
|
||||
ids.sort_unstable();
|
||||
|
||||
let mut total = 0;
|
||||
@@ -37,7 +39,7 @@ fn main() -> Result<(), io::Error> {
|
||||
// range_idx += 1;
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
let mut max_seen = 0;
|
||||
for (low, high) in ranges {
|
||||
let new_low = max(low, max_seen + 1);
|
||||
@@ -46,4 +48,4 @@ fn main() -> Result<(), io::Error> {
|
||||
}
|
||||
println!("Result {}", total);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
99
src/6-trash-compactor.rs
Normal file
99
src/6-trash-compactor.rs
Normal file
@@ -0,0 +1,99 @@
|
||||
use std::{
|
||||
fs::File,
|
||||
io::{self, BufRead, BufReader},
|
||||
};
|
||||
|
||||
enum Operation {
|
||||
Add,
|
||||
Mul,
|
||||
}
|
||||
|
||||
impl Operation {
|
||||
fn apply(&self, values: &[u64]) -> u64 {
|
||||
match self {
|
||||
Operation::Add => values.iter().sum(),
|
||||
Operation::Mul => values.iter().product(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&char> for Operation {
|
||||
fn from(value: &char) -> Self {
|
||||
match value {
|
||||
'+' => Operation::Add,
|
||||
'*' => Operation::Mul,
|
||||
_ => panic!("Unknown operation"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Problem {
|
||||
operation: Operation,
|
||||
values: Vec<u64>,
|
||||
}
|
||||
|
||||
impl Problem {
|
||||
fn solve(&self) -> u64 {
|
||||
// println!("{} {:?}", op, xs);
|
||||
self.operation.apply(&self.values)
|
||||
}
|
||||
}
|
||||
|
||||
fn transpose_chars(input: Vec<String>) -> Vec<Vec<char>> {
|
||||
let height = input[0].len();
|
||||
let mut input_iters: Vec<_> = input.iter().map(|s| s.chars()).collect();
|
||||
|
||||
(0..height)
|
||||
.map(|_| {
|
||||
input_iters
|
||||
.iter_mut()
|
||||
.map(|it| it.next().unwrap())
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
|
||||
fn split_problems(input: &[Vec<char>]) -> Vec<Problem> {
|
||||
let mut problems = Vec::new();
|
||||
input
|
||||
.iter()
|
||||
.rev()
|
||||
.fold(Vec::new(), |mut acc: Vec<u64>, cs| {
|
||||
let Ok(next) = cs[..cs.len() - 1]
|
||||
.iter()
|
||||
.collect::<String>()
|
||||
.trim()
|
||||
.parse::<u64>()
|
||||
else {
|
||||
return acc;
|
||||
};
|
||||
match cs.last() {
|
||||
None => panic!("Oh no!"),
|
||||
Some(' ') => {
|
||||
acc.push(next);
|
||||
acc
|
||||
}
|
||||
Some(op) => {
|
||||
acc.push(next);
|
||||
problems.push(Problem {
|
||||
operation: op.into(),
|
||||
values: acc,
|
||||
});
|
||||
Vec::new()
|
||||
}
|
||||
}
|
||||
});
|
||||
problems
|
||||
}
|
||||
|
||||
fn main() -> Result<(), io::Error> {
|
||||
let f = File::open("6-input.txt")?;
|
||||
let reader = BufReader::new(f);
|
||||
|
||||
let inputs = reader.lines().collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
let inputs_t = transpose_chars(inputs);
|
||||
let total: u64 = split_problems(&inputs_t).iter().map(Problem::solve).sum();
|
||||
println!("Result {}", total);
|
||||
Ok(())
|
||||
}
|
||||
44
src/7-laboratories.rs
Normal file
44
src/7-laboratories.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
use std::{
|
||||
fs::File,
|
||||
io::{self, BufRead, BufReader},
|
||||
};
|
||||
|
||||
fn main() -> Result<(), io::Error> {
|
||||
let f = File::open("7-input.txt")?;
|
||||
let reader = BufReader::new(f);
|
||||
|
||||
let lines: Vec<_> = reader.lines().collect::<Result<_, _>>()?;
|
||||
let initial_state: Vec<_> = lines[0]
|
||||
.chars()
|
||||
.map(|c| if c == 'S' { 1 } else { 0 })
|
||||
.collect();
|
||||
|
||||
let (final_state, splits) =
|
||||
lines[1..]
|
||||
.iter()
|
||||
.fold((initial_state, 0u64), |(state, mut splits), row| {
|
||||
let mut next_state = vec![0; state.len()];
|
||||
for (i, c) in row.chars().enumerate() {
|
||||
match (c, state[i]) {
|
||||
(_, 0) => (),
|
||||
('.', x) => {
|
||||
next_state[i] += x;
|
||||
}
|
||||
('^', x) => {
|
||||
splits += 1;
|
||||
if i > 0 {
|
||||
next_state[i - 1] += x;
|
||||
}
|
||||
if i < state.len() - 1 {
|
||||
next_state[i + 1] += x;
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
(next_state, splits)
|
||||
});
|
||||
let paths: u64 = final_state.iter().sum();
|
||||
println!("Result: {} splits, {} paths", splits, paths);
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user