diff --git a/09/main.rs b/09/main.rs new file mode 100644 index 0000000..3d9cc04 --- /dev/null +++ b/09/main.rs @@ -0,0 +1,193 @@ +#![allow(dead_code)] +use std::collections::HashMap; +use std::fs::File; +use std::io; +use std::io::BufRead; +use std::path::Path; +use std::time::Instant; + +#[derive(Debug, Eq, Hash, PartialEq, Copy, Clone)] +struct Coord(i32, i32); + +fn main() { + let now = Instant::now(); + part_one(); + let part_one_duration = now.elapsed(); + part_two(); + let part_two_duration = now.elapsed(); + println!( + "p1: {}ms, p2: {}ms", + part_one_duration.as_millis(), + (part_two_duration - part_one_duration).as_millis() + ); +} + +fn read_lines

(filename: P) -> io::Result>> +where + P: AsRef, +{ + // note that this discards a final newline + let file = File::open(filename)?; + Ok(io::BufReader::new(file).lines()) +} + +fn part_one() { + let mut grid = HashMap::::new(); + let (mut head_x, mut head_y) = (0, 0); + let (mut tail_x, mut tail_y) = (0, 0); + grid.insert(Coord(0, 0), true); + if let Ok(lines) = read_lines("./inputs/input") { + for line in lines { + if let Ok(m) = line { + let mut i = m.split(" "); + let dir = i.next().unwrap(); + let steps = i.next().unwrap().parse::().unwrap(); + match dir { + "U" => { + for _ in 0..steps { + head_y += 1; + (tail_x, tail_y) = update_tail(head_x, head_y, tail_x, tail_y); + grid.insert(Coord(tail_x, tail_y), true); + } + } + "D" => { + for _ in 0..steps { + head_y -= 1; + (tail_x, tail_y) = update_tail(head_x, head_y, tail_x, tail_y); + grid.insert(Coord(tail_x, tail_y), true); + } + } + "L" => { + for _ in 0..steps { + head_x -= 1; + (tail_x, tail_y) = update_tail(head_x, head_y, tail_x, tail_y); + grid.insert(Coord(tail_x, tail_y), true); + } + } + "R" => { + for _ in 0..steps { + head_x += 1; + (tail_x, tail_y) = update_tail(head_x, head_y, tail_x, tail_y); + grid.insert(Coord(tail_x, tail_y), true); + } + } + _ => { + println!("unknown instruction {:?}", m); + } + } + } + } + } + println!("{}", grid.len()); +} + +fn part_two() { + let mut grid = HashMap::::new(); + let mut knots: Vec = vec![ + Coord(0, 0), + Coord(0, 0), + Coord(0, 0), + Coord(0, 0), + Coord(0, 0), + Coord(0, 0), + Coord(0, 0), + Coord(0, 0), + Coord(0, 0), + Coord(0, 0), + ]; + if let Ok(lines) = read_lines("./inputs/input") { + for line in lines { + if let Ok(m) = line { + let mut i = m.split(" "); + let dir = i.next().unwrap(); + let steps = i.next().unwrap().parse::().unwrap(); + match dir { + "U" => { + for _ in 0..steps { + knots[0] = Coord(knots[0].0, knots[0].1 + 1); + for j in 0..9 { + let (tail_x, tail_y) = update_tail( + knots[j].0, + knots[j].1, + knots[j + 1].0, + knots[j + 1].1, + ); + knots[j + 1] = Coord(tail_x, tail_y); + } + grid.insert(knots[9], true); + } + } + "D" => { + for _ in 0..steps { + knots[0] = Coord(knots[0].0, knots[0].1 - 1); + for j in 0..9 { + let (tail_x, tail_y) = update_tail( + knots[j].0, + knots[j].1, + knots[j + 1].0, + knots[j + 1].1, + ); + knots[j + 1] = Coord(tail_x, tail_y); + } + grid.insert(knots[9], true); + } + } + "L" => { + for _ in 0..steps { + knots[0] = Coord(knots[0].0 - 1, knots[0].1); + for j in 0..9 { + let (tail_x, tail_y) = update_tail( + knots[j].0, + knots[j].1, + knots[j + 1].0, + knots[j + 1].1, + ); + knots[j + 1] = Coord(tail_x, tail_y); + } + grid.insert(knots[9], true); + } + } + "R" => { + for _ in 0..steps { + knots[0] = Coord(knots[0].0 + 1, knots[0].1); + for j in 0..9 { + let (tail_x, tail_y) = update_tail( + knots[j].0, + knots[j].1, + knots[j + 1].0, + knots[j + 1].1, + ); + knots[j + 1] = Coord(tail_x, tail_y); + } + grid.insert(knots[9], true); + } + } + _ => { + println!("unknown instruction {:?}", m); + } + } + } + } + } + println!("{}", grid.len()); +} + +fn update_tail(head_x: i32, head_y: i32, mut tail_x: i32, mut tail_y: i32) -> (i32, i32) { + if head_y - tail_y == 2 { + tail_y += 1; + tail_x = head_x; + } + if tail_y - head_y == 2 { + tail_y -= 1; + tail_x = head_x; + } + if head_x - tail_x == 2 { + tail_x += 1; + tail_y = head_y; + } + if tail_x - head_x == 2 { + tail_x -= 1; + tail_y = head_y; + } + return (tail_x, tail_y); +} diff --git a/10/main.go b/10/main.go new file mode 100644 index 0000000..7aea35f --- /dev/null +++ b/10/main.go @@ -0,0 +1,111 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" + "strings" + "time" +) + +func mustAtoi(line string) int { + i, _ := strconv.Atoi(line) + return i +} + +func main() { + start := time.Now() + partOne() + duration := time.Since(start) + partTwo() + duration2 := time.Since(start) + fmt.Printf("p1: %s, p2: %s\n", duration, duration2-duration) +} + +func makeScanner(test bool) *bufio.Scanner { + var f *os.File + if test { + f, _ = os.Open("inputs/testinput") + } else { + f, _ = os.Open("inputs/input") + } + reader := bufio.NewReader(f) + return bufio.NewScanner(reader) +} + +func partOne() { + scanner := makeScanner(false) + + register := 1 + cycleCount := 0 + signalStrength := 0 + + for scanner.Scan() { + line := scanner.Text() + switch { + case line == "noop": + cycleCount++ + if cycleCount%40 == 20 { + signalStrength += (cycleCount) * register + } + default: + s := strings.Split(line, " ") + x := mustAtoi(s[1]) + cycleCount++ + if cycleCount%40 == 20 { + signalStrength += (cycleCount) * register + } + cycleCount++ + if cycleCount%40 == 20 { + signalStrength += (cycleCount) * register + } + register += x + } + } + fmt.Println(signalStrength) +} + +func partTwo() { + scanner := makeScanner(false) + + screen := make([]string, 241) + + register := 1 + cycleCount := 0 + + for scanner.Scan() { + line := scanner.Text() + switch { + case line == "noop": + if register == cycleCount%40 || register-1 == cycleCount%40 || register+1 == cycleCount%40 { + screen[cycleCount] = "#" + } else { + screen[cycleCount] = "." + } + cycleCount++ + default: + s := strings.Split(line, " ") + x := mustAtoi(s[1]) + if register == cycleCount%40 || register-1 == cycleCount%40 || register+1 == cycleCount%40 { + screen[cycleCount] = "#" + } else { + screen[cycleCount] = "." + } + cycleCount++ + if register == cycleCount%40 || register-1 == cycleCount%40 || register+1 == cycleCount%40 { + screen[cycleCount] = "#" + } else { + screen[cycleCount] = "." + } + cycleCount++ + register += x + } + } + fmt.Println(screen[1:40]) + fmt.Println(screen[41:80]) + fmt.Println(screen[81:120]) + fmt.Println(screen[121:160]) + fmt.Println(screen[161:200]) + fmt.Println(screen[201:240]) +} diff --git a/10/main.rs b/10/main.rs new file mode 100644 index 0000000..a49eaf0 --- /dev/null +++ b/10/main.rs @@ -0,0 +1,38 @@ +#![allow(dead_code)] +use std::fs::File; +use std::io; +use std::io::BufRead; +use std::path::Path; +use std::time::Instant; + +fn main() { + let now = Instant::now(); + part_one(); + let part_one_duration = now.elapsed(); + part_two(); + let part_two_duration = now.elapsed(); + println!( + "p1: {}ms, p2: {}ms", + part_one_duration.as_millis(), + (part_two_duration - part_one_duration).as_millis() + ); +} + +fn read_lines

(filename: P) -> io::Result>> +where + P: AsRef, +{ + // note that this discards a final newline + let file = File::open(filename)?; + Ok(io::BufReader::new(file).lines()) +} + +fn part_one() { + if let Ok(lines) = read_lines("./inputs/input") { + for line in lines { + // do stuff + } + } +} + +fn part_two() {}