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/input/day13.txt | 43 ++++++++++++++++++++++++++++++++ 2017/src/bin/day13.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 2017/input/day13.txt create mode 100644 2017/src/bin/day13.rs diff --git a/2017/input/day13.txt b/2017/input/day13.txt new file mode 100644 index 0000000..37916d9 --- /dev/null +++ b/2017/input/day13.txt @@ -0,0 +1,43 @@ +0: 3 +1: 2 +2: 4 +4: 4 +6: 5 +8: 6 +10: 6 +12: 8 +14: 6 +16: 6 +18: 9 +20: 8 +22: 8 +24: 8 +26: 12 +28: 8 +30: 12 +32: 12 +34: 12 +36: 10 +38: 14 +40: 12 +42: 10 +44: 8 +46: 12 +48: 14 +50: 12 +52: 14 +54: 14 +56: 14 +58: 12 +62: 14 +64: 12 +66: 12 +68: 14 +70: 14 +72: 14 +74: 17 +76: 14 +78: 18 +84: 14 +90: 20 +92: 14 \ No newline at end of file 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 ent'>
Commit message (Expand)Author
2021-01-13Add gg mapping to viJune McEnroe
2021-01-13Match \ before newline in string as EscapeJune McEnroe
2021-01-13Remove -t flag from ctags commandsJune McEnroe
2021-01-13Match strftime format specifiersJune McEnroe
2021-01-13Switch git.causal.agency to hilex and htagmlJune McEnroe
2021-01-13Try to return make substitutions as single tokensJune McEnroe
2021-01-13Shorten hilex class namesJune McEnroe
2021-01-13Move text "lexer" to hilex.cJune McEnroe
2021-01-13Try to return strings as single tokensJune McEnroe
2021-01-13Match [] as Operator in C lexerJune McEnroe
2021-01-13Fix C lexer to require a digit in a float literalJune McEnroe
2021-01-13Support long double in c.shJune McEnroe
2021-01-13Update Terminal.app coloursJune McEnroe
2021-01-13Increase dark white brightness slightlyJune McEnroe
2021-01-13Add hilex example to htagml manualJune McEnroe
2021-01-12Style causal.agency like bin HTMLJune McEnroe
2021-01-12Avoid matching tag text inside HTML elementsJune McEnroe
2021-01-12Use hilex for up -hJune McEnroe
2021-01-12Use hilex for bin HTMLJune McEnroe
2021-01-12Don't output a pre in hilex by defaultJune McEnroe
2021-01-12Move hilex out of hilex directoryJune McEnroe
2021-01-12Consolidate hilex formatters into hilex.cJune McEnroe
2021-01-12Remove hacky tagging from hilexJune McEnroe
2021-01-12Add htagml -iJune McEnroe
2021-01-12Render tag index in HTMLJune McEnroe
2021-01-12Add htagml -xJune McEnroe
2021-01-12Prevent matching the same tag twiceJune McEnroe
2021-01-12Process htagml file line by lineJune McEnroe
2021-01-12Split fields by tab onlyJune McEnroe
2021-01-12List both Makefile and html.sh under README.7June McEnroe
2021-01-12Add htagml exampleJune McEnroe
2021-01-12Use mandoc and htagml for bin htmlJune McEnroe
2021-01-12Add htagmlJune McEnroe
2021-01-12Replace causal.agency with a simple mdoc pageJune McEnroe
2021-01-11Publish "Using vi"June McEnroe
2021-01-11Enable diff.colorMovedJune McEnroe
2021-01-10Set less search case-insensitiveJune McEnroe
2021-01-10Set EXINITJune McEnroe
2021-01-09Add c -t flag to print expression typeJune McEnroe
2021-01-05Update taglineJune McEnroe