Bladeren bron

Day 1 and project scaffolding

master
Chris Smith 4 jaren geleden
commit
d6fc4e29f2
Getekend door: Chris Smith <chris@chameth.com> GPG sleutel-ID: 3A2D4BBDC4A3C9A9
11 gewijzigde bestanden met toevoegingen van 277 en 0 verwijderingen
  1. 5
    0
      .gitignore
  2. 2
    0
      01/answers.txt
  3. 100
    0
      01/input.txt
  4. 40
    0
      01/main.go
  5. 46
    0
      LICENCE.md
  6. 17
    0
      README.adoc
  7. 30
    0
      common/io.go
  8. 7
    0
      docker/Dockerfile
  9. 13
    0
      docker/entrypoint.sh
  10. 3
    0
      go.mod
  11. 14
    0
      run.sh

+ 5
- 0
.gitignore Bestand weergeven

@@ -0,0 +1,5 @@
1
+/.idea
2
+/.vscode
3
+/venv
4
+/*.exe
5
+/day??

+ 2
- 0
01/answers.txt Bestand weergeven

@@ -0,0 +1,2 @@
1
+3173518
2
+4757427

+ 100
- 0
01/input.txt Bestand weergeven

@@ -0,0 +1,100 @@
1
+131787
2
+116597
3
+71331
4
+101986
5
+56538
6
+105039
7
+119405
8
+87762
9
+113957
10
+69613
11
+63698
12
+117674
13
+72876
14
+105026
15
+83620
16
+132592
17
+137403
18
+96832
19
+58387
20
+97609
21
+50978
22
+52896
23
+145584
24
+140832
25
+74504
26
+52998
27
+64722
28
+143334
29
+89601
30
+89326
31
+85906
32
+117840
33
+91299
34
+50593
35
+74470
36
+141591
37
+61069
38
+130479
39
+69195
40
+77411
41
+106137
42
+80954
43
+117644
44
+113063
45
+127587
46
+148770
47
+71286
48
+123430
49
+133562
50
+121053
51
+64311
52
+52818
53
+148583
54
+107511
55
+92838
56
+79724
57
+122022
58
+122602
59
+50344
60
+56938
61
+102363
62
+123140
63
+105469
64
+72773
65
+96023
66
+53669
67
+70394
68
+100930
69
+55213
70
+53756
71
+62225
72
+57172
73
+56049
74
+64661
75
+112321
76
+59872
77
+111597
78
+115958
79
+105468
80
+62111
81
+72865
82
+80323
83
+103897
84
+137687
85
+70178
86
+113314
87
+122121
88
+128654
89
+136723
90
+77279
91
+104806
92
+103491
93
+92168
94
+119263
95
+128791
96
+102237
97
+86578
98
+92728
99
+104785
100
+116658

+ 40
- 0
01/main.go Bestand weergeven

@@ -0,0 +1,40 @@
1
+package main
2
+
3
+import (
4
+	"fmt"
5
+	"github.com/csmith/aoc-2019/common"
6
+	"strconv"
7
+)
8
+
9
+func fuel(mass int) int {
10
+	return (mass / 3) - 2
11
+}
12
+
13
+func fuelRequirements(input []string) (int, int) {
14
+	var (
15
+		simpleTotal = 0
16
+		recursiveTotal = 0
17
+	)
18
+
19
+	for _, line := range input {
20
+		mass, err := strconv.Atoi(line)
21
+		if err != nil {
22
+			panic(err)
23
+		}
24
+
25
+		required := fuel(mass)
26
+		simpleTotal += required
27
+		for required > 0 {
28
+			recursiveTotal += required
29
+			required = fuel(required)
30
+		}
31
+	}
32
+	return simpleTotal, recursiveTotal
33
+}
34
+
35
+func main() {
36
+	input := common.ReadLines("01/input.txt")
37
+	part1, part2 := fuelRequirements(input)
38
+	fmt.Println(part1)
39
+	fmt.Println(part2)
40
+}

+ 46
- 0
LICENCE.md Bestand weergeven

@@ -0,0 +1,46 @@
1
+# CC0 1.0 Universal
2
+
3
+```
4
+CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES
5
+NOT CREATE AN ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS INFORMATION ON AN "AS-IS" BASIS. CREATIVE
6
+COMMONS MAKES NO WARRANTIES REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED HEREUNDER, AND
7
+DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED HEREUNDER.
8
+```
9
+
10
+### Statement of Purpose
11
+
12
+The laws of most jurisdictions throughout the world automatically confer exclusive Copyright and Related Rights (defined below) upon the creator and subsequent owner(s) (each and all, an "owner") of an original work of authorship and/or a database (each, a "Work").
13
+
14
+Certain owners wish to permanently relinquish those rights to a Work for the purpose of contributing to a commons of creative, cultural and scientific works ("Commons") that the public can reliably and without fear of later claims of infringement build upon, modify, incorporate in other works, reuse and redistribute as freely as possible in any form whatsoever and for any purposes, including without limitation commercial purposes. These owners may contribute to the Commons to promote the ideal of a free culture and the further production of creative, cultural and scientific works, or to gain reputation or greater distribution for their Work in part through the use and efforts of others.
15
+
16
+For these and/or other purposes and motivations, and without any expectation of additional consideration or compensation, the person associating CC0 with a Work (the "Affirmer"), to the extent that he or she is an owner of Copyright and Related Rights in the Work, voluntarily elects to apply CC0 to the Work and publicly distribute the Work under its terms, with knowledge of his or her Copyright and Related Rights in the Work and the meaning and intended legal effect of CC0 on those rights.
17
+
18
+1. __Copyright and Related Rights.__ A Work made available under CC0 may be protected by copyright and related or neighboring rights ("Copyright and Related Rights"). Copyright and Related Rights include, but are not limited to, the following:
19
+
20
+    i. the right to reproduce, adapt, distribute, perform, display, communicate, and translate a Work;
21
+
22
+    ii. moral rights retained by the original author(s) and/or performer(s);
23
+
24
+    iii. publicity and privacy rights pertaining to a person's image or likeness depicted in a Work;
25
+
26
+    iv. rights protecting against unfair competition in regards to a Work, subject to the limitations in paragraph 4(a), below;
27
+
28
+    v. rights protecting the extraction, dissemination, use and reuse of data in a Work;
29
+
30
+    vi. database rights (such as those arising under Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, and under any national implementation thereof, including any amended or successor version of such directive); and
31
+
32
+    vii. other similar, equivalent or corresponding rights throughout the world based on applicable law or treaty, and any national implementations thereof.
33
+
34
+2. __Waiver.__ To the greatest extent permitted by, but not in contravention of, applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and unconditionally waives, abandons, and surrenders all of Affirmer's Copyright and Related Rights and associated claims and causes of action, whether now known or unknown (including existing as well as future claims and causes of action), in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each member of the public at large and to the detriment of Affirmer's heirs and successors, fully intending that such Waiver shall not be subject to revocation, rescission, cancellation, termination, or any other legal or equitable action to disrupt the quiet enjoyment of the Work by the public as contemplated by Affirmer's express Statement of Purpose.
35
+
36
+3. __Public License Fallback.__ Should any part of the Waiver for any reason be judged legally invalid or ineffective under applicable law, then the Waiver shall be preserved to the maximum extent permitted taking into account Affirmer's express Statement of Purpose. In addition, to the extent the Waiver is so judged Affirmer hereby grants to each affected person a royalty-free, non transferable, non sublicensable, non exclusive, irrevocable and unconditional license to exercise Affirmer's Copyright and Related Rights in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "License"). The License shall be deemed effective as of the date CC0 was applied by Affirmer to the Work. Should any part of the License for any reason be judged legally invalid or ineffective under applicable law, such partial invalidity or ineffectiveness shall not invalidate the remainder of the License, and in such case Affirmer hereby affirms that he or she will not (i) exercise any of his or her remaining Copyright and Related Rights in the Work or (ii) assert any associated claims and causes of action with respect to the Work, in either case contrary to Affirmer's express Statement of Purpose.
37
+
38
+4. __Limitations and Disclaimers.__
39
+
40
+    a. No trademark or patent rights held by Affirmer are waived, abandoned, surrendered, licensed or otherwise affected by this document.
41
+
42
+    b. Affirmer offers the Work as-is and makes no representations or warranties of any kind concerning the Work, express, implied, statutory or otherwise, including without limitation warranties of title, merchantability, fitness for a particular purpose, non infringement, or the absence of latent or other defects, accuracy, or the present or absence of errors, whether or not discoverable, all to the greatest extent permissible under applicable law.
43
+
44
+    c. Affirmer disclaims responsibility for clearing rights of other persons that may apply to the Work or any use thereof, including without limitation any person's Copyright and Related Rights in the Work. Further, Affirmer disclaims responsibility for obtaining any necessary consents, permissions or other rights required for any use of the Work.
45
+
46
+    d. Affirmer understands and acknowledges that Creative Commons is not a party to this document and has no duty or obligation with respect to this CC0 or use of the Work.

+ 17
- 0
README.adoc Bestand weergeven

@@ -0,0 +1,17 @@
1
+= Advent of Code 2019
2
+
3
+This repository contains my solution to 2019's https://adventofcode.com/2018[Advent of Code] puzzles in Go.
4
+
5
+The easiest way to run these is using docker – simply execute `run.sh &lt;day&gt;` to build a docker image and execute
6
+the specific day.
7
+
8
+I have separate repositories for solutions from previous years:
9
+ - https://g.c5h.io/archive/aoc-2018[2018] (Nim)
10
+ - https://g.c5h.io/archive/aoc-2017[2017] (Python)
11
+ - https://g.c5h.io/archive/aoc-2016[2016] (Python)
12
+ - https://g.c5h.io/archive/aoc-2015[2015] (Python)
13
+
14
+'''
15
+
16
+To the extent possible under law, I waive all copyright and related or neighboring rights to this work. This work is
17
+published from the United Kingdom. See link:LICENCE.md[LICENCE.md] for full details.

+ 30
- 0
common/io.go Bestand weergeven

@@ -0,0 +1,30 @@
1
+package common
2
+
3
+import (
4
+	"bufio"
5
+	"os"
6
+)
7
+
8
+// ReadLines reads all lines from the given path and returns them in a slice of strings.
9
+// If an error occurs, the function will panic.
10
+func ReadLines(path string) []string {
11
+	file, err := os.Open(path)
12
+	if err != nil {
13
+		panic(err)
14
+	}
15
+	defer func() {
16
+		_ = file.Close()
17
+	}()
18
+
19
+	var lines []string
20
+	scanner := bufio.NewScanner(file)
21
+	for scanner.Scan() {
22
+		lines = append(lines, scanner.Text())
23
+	}
24
+
25
+	if scanner.Err() != nil {
26
+		panic(scanner.Err())
27
+	}
28
+
29
+	return lines
30
+}

+ 7
- 0
docker/Dockerfile Bestand weergeven

@@ -0,0 +1,7 @@
1
+FROM golang:1.13
2
+
3
+ADD entrypoint.sh /entrypoint.sh
4
+RUN chmod +x /entrypoint.sh
5
+
6
+CMD /entrypoint.sh
7
+VOLUME /code

+ 13
- 0
docker/entrypoint.sh Bestand weergeven

@@ -0,0 +1,13 @@
1
+#!/bin/bash
2
+
3
+set -e
4
+cd /code
5
+
6
+if [ -d "$1" ]; then
7
+    if [ ! -f "${GOPATH}/bin/$1" ] || [ "${GOPATH}/bin/$1" -nt "$1/main.go" ]; then
8
+        export HOME="/tmp"
9
+        go get -d -v ./... >/dev/null
10
+        go install ./... >/dev/null
11
+    fi
12
+    time "${GOPATH}/bin/$1"
13
+fi

+ 3
- 0
go.mod Bestand weergeven

@@ -0,0 +1,3 @@
1
+module github.com/csmith/aoc-2019
2
+
3
+go 1.12

+ 14
- 0
run.sh Bestand weergeven

@@ -0,0 +1,14 @@
1
+#!/bin/bash
2
+
3
+IMAGE=csmith/aoc-2019-01
4
+
5
+docker image inspect $IMAGE >/dev/null 2>&1
6
+if [ $? -ne 0 ]
7
+then
8
+    echo "One time setup: building docker image..."
9
+    cd docker
10
+    docker build . -t $IMAGE
11
+    cd ..
12
+fi
13
+
14
+docker run --rm -it -v "$(pwd)":/code $IMAGE /entrypoint.sh $@

Laden…
Annuleren
Opslaan