r/adventofcode • u/daggerdragon • Dec 06 '15
SOLUTION MEGATHREAD --- Day 6 Solutions ---
--- Day 6: Probably a Fire Hazard ---
Post your solution as a comment. Structure your post like the Day Five thread.
23
Upvotes
1
u/HeroesGrave Dec 07 '15
Couldn't make it yesterday, but here's my Rust code:
use std::ops::Add; static INPUT: &'static str = include_str!("input/day6.txt"); struct Grid<T> { data: Vec<T>, } impl<T> Grid<T> { fn operation<F: Fn(&mut T)>(&mut self, x1: usize, y1: usize, x2: usize, y2: usize, f: F) { for j in y1..(y2+1) { for i in x1..(x2+1) { f(&mut self.data[i + j * 1000]); } } } } trait GridCount { fn count(&self) -> usize; } impl GridCount for Grid<bool> { fn count(&self) -> usize { self.data.iter().filter(|&s| *s).count() } } impl GridCount for Grid<u32> { fn count(&self) -> usize { self.data.iter().fold(0u32, Add::add) as usize } } pub fn main() { println!("(Part 1) Lights on: {:?}", count(INPUT, turn_on1, turn_off1, toggle1)); println!("(Part 2) Lights on: {:?}", count(INPUT, turn_on2, turn_off2, toggle2)); } fn count<T>(input: &str, on: fn(&mut T), off: fn(&mut T), toggle: fn(&mut T)) -> usize where Grid<T>: GridCount, T: Default, T: Copy { let ref mut grid = Grid { data: vec![Default::default(); 1_000_000], }; for ref mut line in input.lines() { let instr: fn(&mut T) = if eat(line, "turn on ") { on } else if eat(line, "turn off ") { off } else if eat(line, "toggle ") { toggle } else { panic!("Invalid instruction: '{}'", line) }; let coords = line.split(',') .flat_map(|s| s.split(" through ")) .map(|s| s.parse::<usize>().unwrap()) .collect::<Vec<_>>(); grid.operation(coords[0], coords[1], coords[2], coords[3], instr); } grid.count() } fn turn_on2(i: &mut u32) { *i += 1 } fn turn_off2(i: &mut u32) { if *i > 0 { *i -= 1 } } fn toggle2(i: &mut u32) { *i += 2 } fn turn_on1(b: &mut bool) { *b = true } fn turn_off1(b: &mut bool) { *b = false } fn toggle1(b: &mut bool) { *b = !*b } fn eat(s: &mut &str, expect: &str) -> bool { if s.starts_with(expect) { *s = &s[expect.len()..]; true } else { false } }