day 10
This commit is contained in:
111
10/main.go
Normal file
111
10/main.go
Normal file
@@ -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])
|
||||
}
|
38
10/main.rs
Normal file
38
10/main.rs
Normal file
@@ -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<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
// 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() {}
|
Reference in New Issue
Block a user