Browse Source

[2019-12-02] Add solutions for day02 part 1

master
Elis Hirwing 2 years ago
parent
commit
79d7f6e4b4
Signed by: etu GPG Key ID: D57EFA625C9A925F
  1. 2
      Makefile
  2. 3
      day02/go.mod
  3. 1
      day02/input.txt
  4. 91
      day02/main.go

2
Makefile

@ -10,6 +10,8 @@ day01:
rm result*
day02:
go run day02/main.go day02/input.txt
day03:
day04:
day05:

3
day02/go.mod

@ -0,0 +1,3 @@
module git.elis.nu/etu/aoc2019/day02
go 1.15

1
day02/input.txt

@ -0,0 +1 @@
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,6,19,1,9,19,23,2,23,10,27,1,27,5,31,1,31,6,35,1,6,35,39,2,39,13,43,1,9,43,47,2,9,47,51,1,51,6,55,2,55,10,59,1,59,5,63,2,10,63,67,2,9,67,71,1,71,5,75,2,10,75,79,1,79,6,83,2,10,83,87,1,5,87,91,2,9,91,95,1,95,5,99,1,99,2,103,1,103,13,0,99,2,14,0,0

91
day02/main.go

@ -0,0 +1,91 @@
package main // import "git.elis.nu/etu/aoc2019/day02"
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
)
func main() {
log.Println("Starting program to solve 2019-12-02")
// Store time to print out execution time
start := time.Now()
var entries []int
// Get file handler
file, err := os.Open(os.Args[1])
defer file.Close()
if err != nil {
log.Fatal("Failed to open file:", err)
}
// Begin parsing file
scanner := bufio.NewScanner(file)
for scanner.Scan() {
for _, raw := range strings.Split(scanner.Text(), ",") {
item, err := strconv.Atoi(raw)
if err != nil {
log.Println("Failed to parse integer:", err)
}
entries = append(entries, item)
}
}
// Print eventual error from parsing the file
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
// Log spent time
log.Printf("Spent %s on parsing input\n", time.Since(start))
start = time.Now()
part1(entries)
log.Printf("Spent %s on solving part1\n", time.Since(start))
start = time.Now()
part2(entries)
log.Printf("Spent %s on solving part2\n", time.Since(start))
}
func part1(entries []int) {
count := len(entries)
key := 0
entries[1] = 12
entries[2] = 2
for key < count {
switch entries[key] {
case 1:
entries[entries[key+3]] = entries[entries[key+1]] + entries[entries[key+2]]
key += 3
case 2:
entries[entries[key+3]] = entries[entries[key+1]] * entries[entries[key+2]]
key += 3
case 99:
key = count
}
key++
}
log.Printf("Part 1 solution: %d \n", entries[0])
}
func part2(entries []int) {
fmt.Println(entries)
}
Loading…
Cancel
Save