From 221aee4693c8af3422c0881bcb2770010a403c58 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Sat, 23 Dec 2017 17:19:04 -0500 Subject: Day 13 --- 2017/src/bin/day13.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 2017/src/bin/day13.rs (limited to '2017/src/bin') diff --git a/2017/src/bin/day13.rs b/2017/src/bin/day13.rs new file mode 100644 index 0000000..360e266 --- /dev/null +++ b/2017/src/bin/day13.rs @@ -0,0 +1,68 @@ +use std::io::{self, Read}; + +#[derive(Debug, Clone, Copy)] +struct Layer { + range: i32, + scanner: i32, + direction: i32, +} + +impl Layer { + fn new(range: i32) -> Self { + Layer { range, scanner: 0, direction: 1 } + } + + fn step(&mut self) { + if self.scanner == 0 { + self.direction = 1; + } else if self.scanner == self.range - 1 { + self.direction = -1; + } + self.scanner += self.direction; + } +} + +fn solve1(input: &str) -> usize { + let mut layers = vec![]; + for line in input.lines() { + let mut iter = line.split(": "); + let index: usize = iter.next().unwrap().parse().unwrap(); + let range = iter.next().unwrap().parse().unwrap(); + layers.resize(index + 1, None); + layers[index] = Some(Layer::new(range)); + } + + let mut severity = 0; + for i in 0..layers.len() { + if let Some(ref layer) = layers[i] { + if layer.scanner == 0 { + severity += i * layer.range as usize; + } + } + + for layer in &mut layers { + layer.as_mut().map(Layer::step); + } + } + + severity +} + +fn main() { + let mut input = String::new(); + io::stdin().read_to_string(&mut input).unwrap(); + + println!("Part 1: {}", solve1(&input)); +} + +#[test] +fn part1() { + assert_eq!(24, solve1( +"\ +0: 3 +1: 2 +4: 4 +6: 4 +" + )); +} -- cgit 1.4.1 tion>
path: root/www/photo.causal.agency/2024-08-22/000044750027.txt (unfollow)
Commit message (Expand)Author
2025-03-25Add emoji for lens, replace all f/'sJune McEnroe
2024-12-29Add photos from December 28June McEnroe
2024-12-29Fancify the manual lens textJune McEnroe
2024-12-28Update film preset list with what I expect to keep shootingJune McEnroe
2024-12-28Add the past few months of rolls of filmJune McEnroe
2024-11-13Add Tea with the Black DragonJune McEnroe
2024-11-05Add a bunch of photosJune McEnroe
2024-10-12Publish "film review"June McEnroe
2024-10-12Add photos from October 6June McEnroe
2024-10-12Add photos from October 5June McEnroe
2024-10-09Add photos from October 1June McEnroe
2024-10-07Add photos from September 29June McEnroe
2024-10-07Add photos from September 28June McEnroe
2024-10-07Add photos from September 22June McEnroe
2024-09-25Add photos from September 15June McEnroe
2024-09-24Add photos from September 14June McEnroe
2024-09-24Add photos from September 12June McEnroe
2024-09-24Add photos from September 7June McEnroe
2024-09-24Allow not having descriptionsJune McEnroe
2024-09-23Automatically select the last used lens for a bodyJune McEnroe
2024-09-19Add photos from September 5June McEnroe
2024-09-15Add some more film stocks to the listJune McEnroe
2024-09-13Add photos from September 2June McEnroe
2024-09-13Add Fomapan 200 to films listJune McEnroe
2024-09-10Add August 29 picnic photosJune McEnroe
2024-09-08Apply some bold to trips renderingJune McEnroe
2024-09-08Render trips hopefully more efficientlyJune McEnroe
2024-09-08Allow removing bodies and lensesJune McEnroe
2024-09-08Limit body width so it looks less silly on desktopJune McEnroe
2024-09-07Handle no film being loadedJune McEnroe
2024-09-07Fancy up the text a littleJune McEnroe