Browse Source

[2019-12-01] Add solutions for day01

master
Elis Hirwing 2 years ago
parent
commit
bca21faad8
Signed by: etu GPG Key ID: D57EFA625C9A925F
  1. 6
      Makefile
  2. 4
      day01/example.txt
  3. 3
      day01/go.mod
  4. 100
      day01/input.txt
  5. 88
      day01/main.go
  6. 24
      day01/puzzle1.nix
  7. 31
      day01/puzzle2.nix

6
Makefile

@ -3,6 +3,12 @@
all: day01 day02 day03 day04 day05 day06 day07 day08 day09 day10 day11 day12 day13 day14 day15 day16 day17 day18 day19 day20 day21 day22 day23 day24 day25
day01:
go run day01/main.go day01/input.txt
nix-build day01/puzzle1.nix day01/puzzle2.nix
cat result result-2
rm result*
day02:
day03:
day04:

4
day01/example.txt

@ -0,0 +1,4 @@
12
14
1969
100756

3
day01/go.mod

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

100
day01/input.txt

@ -0,0 +1,100 @@
129880
115705
118585
124631
81050
138183
61173
95354
130788
89082
75554
110104
140528
71783
125889
126602
73089
76822
51774
85940
81004
149584
145921
105570
142370
80823
147779
115651
70250
67763
128192
51298
134963
73510
90976
141216
65134
140468
143998
101711
88477
53335
138328
141186
149804
64950
53107
54648
97557
85927
125038
80514
64912
140591
114229
57089
123464
127572
137169
146550
51138
115504
128034
147244
108107
101205
51498
136829
140171
59441
144489
139384
145841
96771
116821
88599
126780
65012
67621
129699
149639
97590
147527
117462
146709
60527
107643
92956
72177
92285
62475
63099
66904
77268
62945
134364
106924
117842
130016
123712

88
day01/main.go

@ -0,0 +1,88 @@
package main // import "git.elis.nu/etu/aoc2019/day01"
import (
"bufio"
"log"
"os"
"strconv"
"time"
)
func main() {
log.Println("Starting program to solve 2019-12-01")
// Store time to print out execution time
start := time.Now()
var rows []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() {
row, err := strconv.Atoi(scanner.Text())
if err != nil {
log.Println("Failed to parse int:", err)
}
rows = append(rows, row)
}
// 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(rows)
log.Printf("Spent %s on solving part1\n", time.Since(start))
start = time.Now()
part2(rows)
log.Printf("Spent %s on solving part2\n", time.Since(start))
}
func part1(rows []int) {
sum := 0
for _, row := range rows {
sum = sum + (row/3 - 2)
}
log.Printf("Part 1 solution: We need %d fuel\n", sum)
}
func part2(rows []int) {
sum := 0
for _, row := range rows {
sum = sum + part2costForFuel(row)
}
log.Printf("Part 1 solution: We need %d fuel\n", sum)
}
func part2cost(fuel int) int {
if fuel < 1 {
return 0
}
return fuel + part2costForFuel(fuel)
}
func part2costForFuel(fuel int) int {
return part2cost(fuel/3 - 2)
}

24
day01/puzzle1.nix

@ -0,0 +1,24 @@
with import <nixpkgs> {};
let
# Read input
input = lib.fileContents ./input.txt;
# Split input on \n and do type conversion to integers on all entries
inputRows = map (x: lib.toInt x) (lib.splitString "\n" input);
# Calculate total cost for given amount of fuel
costForFuel = x: x / 3 - 2;
# Do calculation on all entries
calculatedRows = map costForFuel inputRows;
# Sum the list
sum = lib.foldl' (x: y: x + y) 0 calculatedRows;
# Save to file
in writeText "aoc-result-2019-12-01.01" ''
Result for 2019-12-01.01: ${toString sum}
''

31
day01/puzzle2.nix

@ -0,0 +1,31 @@
with import <nixpkgs> {};
let
# Read input
input = lib.fileContents ./input.txt;
# Split input on \n and do type conversion to integers on all entries
inputRows = map (x: lib.toInt x) (lib.splitString "\n" input);
# Recursive function to calculate the fuel
cost = x: if x < 1
then
0
else
x + costForFuel x;
# Calculate total cost for given amount of fuel
costForFuel = x: cost (x / 3 - 2);
# Do calculation on all entries
calculatedRows = map costForFuel inputRows;
# Sum the list
sum = lib.foldl' (x: y: x + y) 0 calculatedRows;
# Save to file
in writeText "aoc-result-2019-12-01.02" ''
Result for 2019-12-01.02: ${toString sum}
''
Loading…
Cancel
Save