Skip to content

Commit c3f070a

Browse files
author
avokadoen
committed
Merge branch 'main' of github.com:Arxcis/adventofcode2020 into main
2 parents d609036 + 6659217 commit c3f070a

File tree

10 files changed

+1446
-23
lines changed

10 files changed

+1446
-23
lines changed

README.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@ Welcome to this community project, where we collaboratively solve the 2020 editi
55

66
## Solutions per language per day
77

8-
| Language | Total | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 |11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|
9-
|-----------|--------|------|-----|-----|------|-----|-----|-----|----|----|----|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
10-
| python3 | **18** | 2 | 1 | 3 | 4 | 4 | 3 | 1 | | | ||||||||||||||||
11-
| golang | **12** | 2 | 2 | 2 | 1 | 2 | 2 | 1 | | | ||||||||||||||||
12-
| sml | **7** | 1 | 1 | 1 | 1 | 1 | 1 | 1 | | | ||||||||||||||||
13-
| deno.ts | **7** | 1 | 1 | 1 | 1 | 1 | 1 | 1 | | | ||||||||||||||||
14-
| c | **6** | 1 | 1 | 1 | 1 | 1 | 1 | | | | ||||||||||||||||
15-
| node.js | **4** | 1 | | | 1 | | 1 | 1 | | | ||||||||||||||||
16-
| zig | **5** | 1 | 1 | 1 | 1 | 1 | | | | | ||||||||||||||||
17-
| c++ | **2** | 1 | 1 | | | | | | | | ||||||||||||||||
18-
| ruby | **2** | 1 | | | 1 | | | | | | ||||||||||||||||
19-
| rust | **1** | 1 | | | | | | | | | ||||||||||||||||
20-
| bash | **0** | | | | | | | | | | ||||||||||||||||
21-
| java | **0** | | | | | | | | | | ||||||||||||||||
22-
| php | **0** | | | | | | | | | | ||||||||||||||||
23-
| **Total** | **61** |**12**|**8**|**8**|**10**|**9**|**9**|**5**| | | ||||||||||||||||
24-
25-
*Last updated: 2020-12-07 22:18:00Z*
8+
| Language | Total | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 |11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|
9+
|-----------|--------|------|-----|-----|------|-----|-----|-----|-----|----|----|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
10+
| python3 | **18** | 2 | 1 | 3 | 4 | 4 | 3 | 1 | | | ||||||||||||||||
11+
| golang | **13** | 2 | 2 | 2 | 1 | 2 | 2 | 1 | 1 | | ||||||||||||||||
12+
| sml | **8** | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | | ||||||||||||||||
13+
| deno.ts | **8** | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | | ||||||||||||||||
14+
| c | **6** | 1 | 1 | 1 | 1 | 1 | 1 | | | | ||||||||||||||||
15+
| zig | **5** | 1 | 1 | 1 | 1 | 1 | | | | | ||||||||||||||||
16+
| node.js | **4** | 1 | | | 1 | | 1 | 1 | | | ||||||||||||||||
17+
| c++ | **2** | 1 | 1 | | | | | | | | ||||||||||||||||
18+
| ruby | **2** | 1 | | | 1 | | | | | | ||||||||||||||||
19+
| rust | **1** | 1 | | | | | | | | | ||||||||||||||||
20+
| bash | **0** | | | | | | | | | | ||||||||||||||||
21+
| java | **0** | | | | | | | | | | ||||||||||||||||
22+
| php | **0** | | | | | | | | | | ||||||||||||||||
23+
| **Total** | **65** |**12**|**8**|**9**|**10**|**9**|**9**|**5**|**3**| | ||||||||||||||||
24+
25+
*Last updated: 2020-12-09 18:56:00Z*
2626

2727
See all languages we support in our [Dockerfile](./Dockerfile).
2828

days/day-08/solutions/.keep

Whitespace-only changes.

days/day-08/solutions/day08.sml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use "../../../util.sml";
2+
3+
local
4+
structure IS = IntSet
5+
in
6+
7+
datatype Op = ACC | JMP | NOP
8+
9+
fun opFromString "acc" = ACC
10+
| opFromString "jmp" = JMP
11+
| opFromString "nop" = NOP
12+
| opFromString s = raise Fail $ "opFromString - bad input: " ^ s
13+
14+
fun toInstruction s =
15+
case String.tokens (fn c => c = #" ") s of
16+
[a, b] => (opFromString a, valOf $ Int.fromString b)
17+
| _ => raise Fail $ "bad input: " ^ s
18+
19+
datatype TerminationReason = STOPPED of int
20+
| INF_LOOP of int
21+
22+
fun programResult (STOPPED n) = n
23+
| programResult (INF_LOOP n) = n
24+
25+
fun run (program : (Op * int) RList.Coll) : TerminationReason =
26+
let fun go seen pc acc =
27+
if IS.contains pc seen then
28+
INF_LOOP acc
29+
else if RList.size program <= pc then
30+
STOPPED acc
31+
else
32+
let val seen' = IS.insert pc seen
33+
in
34+
case RList.get (pc, program) of
35+
(ACC, n) => go seen' (pc + 1) (acc + n)
36+
| (JMP, n) => go seen' (pc + n) acc
37+
| (NOP, _) => go seen' (pc + 1) acc
38+
end
39+
in
40+
go IS.empty 0 0
41+
end
42+
43+
fun flip (ACC, n) = NONE
44+
| flip (JMP, n) = SOME (NOP, n)
45+
| flip (NOP, n) = SOME (JMP, n)
46+
47+
fun findFlip program i =
48+
case flip $ RList.get (i, program) of
49+
NONE => findFlip program (i+1)
50+
| SOME newOp =>
51+
case run (RList.set (i, newOp, program)) of
52+
INF_LOOP _ => findFlip program (i+1)
53+
| STOPPED n => n
54+
55+
fun main () =
56+
let val program = TextIO.inputAll TextIO.stdIn
57+
>> String.tokens (fn c => c = #"\n")
58+
>> List.map toInstruction
59+
>> RList.fromList
60+
in
61+
println $ Int.toString $ programResult $ run program;
62+
println $ Int.toString $ findFlip program 0
63+
end
64+
handle Subscript => println "boo"
65+
end

days/day-08/solutions/day08.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
type Instruction = {
2+
op: "nop" | "acc" | "jmp";
3+
arg: number;
4+
};
5+
6+
const input = (await Deno.readTextFile("/dev/stdin"))
7+
.split("\n")
8+
.slice(0, -1);
9+
10+
const instructions = input
11+
.map((line) => line.split(" "))
12+
.map(([op, arg]) => ({ op, arg: parseInt(arg) })) as Instruction[];
13+
14+
/** --- Part 1 --- */
15+
{
16+
let ipTracker = new Set<number>([]);
17+
let accumulator = 0;
18+
let instructionPointer = 0;
19+
20+
while (!ipTracker.has(instructionPointer)) {
21+
ipTracker.add(instructionPointer);
22+
23+
const { op, arg } = instructions[instructionPointer];
24+
25+
switch (op) {
26+
case "nop":
27+
instructionPointer += 1;
28+
break;
29+
case "acc":
30+
instructionPointer += 1;
31+
accumulator += arg;
32+
break;
33+
case "jmp":
34+
instructionPointer += arg;
35+
break;
36+
}
37+
}
38+
console.log(`${accumulator}`);
39+
}
40+
41+
/** --- Part 2 --- */
42+
{
43+
let ipTracker = new Set<number>([]);
44+
let accumulator = 0;
45+
let instructionPointer = 0;
46+
let modIndex = 0;
47+
48+
for (
49+
let modStep = instructions.findIndex(({ op }) =>
50+
op === "nop" || op === "jmp"
51+
);
52+
modStep !== -1 && instructionPointer < instructions.length;
53+
modStep = instructions.slice(modIndex + 1).findIndex(({ op }) =>
54+
op === "nop" || op === "jmp"
55+
), modIndex += 1 + modStep
56+
) {
57+
ipTracker = new Set<number>([]);
58+
accumulator = 0;
59+
instructionPointer = 0;
60+
61+
const { op: modOp, arg: modArg } = instructions[modIndex];
62+
63+
const modifiedInstructions = [
64+
...instructions.slice(0, modIndex),
65+
{ arg: modArg, op: modOp === "nop" ? "jmp" : "nop" },
66+
...instructions.slice(modIndex + 1),
67+
];
68+
69+
while (
70+
!ipTracker.has(instructionPointer) &&
71+
instructionPointer < instructions.length
72+
) {
73+
ipTracker.add(instructionPointer);
74+
75+
const { op, arg } = modifiedInstructions[instructionPointer];
76+
77+
switch (op) {
78+
case "nop":
79+
instructionPointer += 1;
80+
break;
81+
case "acc":
82+
instructionPointer += 1;
83+
accumulator += arg;
84+
break;
85+
case "jmp":
86+
instructionPointer += arg;
87+
break;
88+
}
89+
}
90+
}
91+
92+
console.log(`${accumulator}`);
93+
}

days/day-08/solutions/tholok97.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"regexp"
8+
"strconv"
9+
)
10+
11+
type instruction struct {
12+
arg int
13+
op string
14+
}
15+
16+
func main() {
17+
scanner := bufio.NewScanner(os.Stdin)
18+
19+
instructions := make([]instruction, 0)
20+
21+
for scanner.Scan() {
22+
line := scanner.Text()
23+
24+
re := regexp.MustCompile(`^(nop|acc|jmp) ([+-]\d+)$`)
25+
matches := re.FindStringSubmatch(line)
26+
27+
arg, err := strconv.Atoi(matches[2])
28+
29+
if err != nil {
30+
panic("Misbehaving number")
31+
}
32+
33+
instructions = append(instructions, instruction{
34+
op: matches[1],
35+
arg: arg,
36+
})
37+
}
38+
39+
_, accumulator := run(instructions)
40+
41+
fmt.Println(accumulator)
42+
43+
for i := 0; i < len(instructions); i++ {
44+
swap := instructions[i].op
45+
46+
switch swap {
47+
case "jmp":
48+
instructions[i].op = "nop"
49+
case "nop":
50+
instructions[i].op = "jmp"
51+
default:
52+
continue
53+
}
54+
55+
isInfinite, accumulator := run(instructions)
56+
57+
if !isInfinite {
58+
fmt.Println(accumulator)
59+
break
60+
}
61+
62+
instructions[i].op = swap
63+
}
64+
}
65+
66+
func run(instructions []instruction) (isinfinite bool, accumulator int) {
67+
curr := 0
68+
69+
visited := make([]bool, len(instructions))
70+
71+
for {
72+
if curr == len(instructions) {
73+
return false, accumulator
74+
}
75+
76+
if visited[curr] {
77+
return true, accumulator
78+
}
79+
80+
visited[curr] = true
81+
82+
ins := instructions[curr]
83+
84+
switch ins.op {
85+
case "acc":
86+
accumulator += ins.arg
87+
curr++
88+
case "nop":
89+
curr++
90+
case "jmp":
91+
curr += ins.arg
92+
}
93+
}
94+
}

days/day-08/test.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ D=$(dirname $(realpath $0))
66

77
echo ""
88
echo "--- Day 8: Handheld Halting ---"
9+
$D/../../languages/go.sh $D/input.txt $D/output.txt $D/solutions/tholok97.go
10+
$D/../../languages/sml.sh $D/input.txt $D/output.txt $D/solutions/day08.sml
11+
$D/../../languages/deno.sh $D/input.txt $D/output.txt $D/solutions/day08.ts
912
echo ""

days/day-09/README.md

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,89 @@
1-
# days/day-09
2-
## --- Part 1 ---
3-
## --- Part 2 ---
1+
# --- Day 9: Encoding Error ---
2+
3+
With your neighbor happily enjoying their video game, you turn your attention to an open data port on the little screen in the seat in front of you.
4+
5+
Though the port is non-standard, you manage to connect it to your computer through the clever use of several paperclips. Upon connection, the port outputs a series of numbers (your puzzle input).
6+
7+
The data appears to be encrypted with the eXchange-Masking Addition System (XMAS) which, conveniently for you, is an old cypher with an important weakness.
8+
9+
XMAS starts by transmitting a preamble of 25 numbers. After that, each number you receive should be the sum of any two of the 25 immediately previous numbers. The two numbers will have different values, and there might be more than one such pair.
10+
11+
For example, suppose your preamble consists of the numbers 1 through 25 in a random order. To be valid, the next number must be the sum of two of those numbers:
12+
```
13+
26 would be a valid next number, as it could be 1 plus 25 (or many other pairs, like 2 and 24).
14+
49 would be a valid next number, as it is the sum of 24 and 25.
15+
100 would not be valid; no two of the previous 25 numbers sum to 100.
16+
50 would also not be valid; although 25 appears in the previous 25 numbers, the two numbers in the pair must be different.
17+
```
18+
19+
Suppose the 26th number is 45, and the first number (no longer an option, as it is more than 25 numbers ago) was 20. Now, for the next number to be valid, there needs to be some pair of numbers among 1-19, 21-25, or 45 that add up to it:
20+
21+
```
22+
26 would still be a valid next number, as 1 and 25 are still within the previous 25 numbers.
23+
65 would not be valid, as no two of the available numbers sum to it.
24+
64 and 66 would both be valid, as they are the result of 19+45 and 21+45 respectively.
25+
```
26+
27+
Here is a larger example which only considers the previous 5 numbers (and has a preamble of length 5):
28+
29+
```
30+
35
31+
20
32+
15
33+
25
34+
47
35+
40
36+
62
37+
55
38+
65
39+
95
40+
102
41+
117
42+
150
43+
182
44+
127
45+
219
46+
299
47+
277
48+
309
49+
576
50+
```
51+
52+
In this example, after the 5-number preamble, almost every number is the sum of two of the previous 5 numbers; the only number that does not follow this rule is 127.
53+
54+
The first step of attacking the weakness in the XMAS data is to find the first number in the list (after the preamble) which is not the sum of two of the 25 numbers before it. What is the first number that does not have this property?
55+
56+
## --- Part Two ---
57+
58+
The final step in breaking the XMAS encryption relies on the invalid number you just found: you must find a contiguous set of at least two numbers in your list which sum to the invalid number from step 1.
59+
60+
Again consider the above example:
61+
62+
```
63+
35
64+
20
65+
15
66+
25
67+
47
68+
40
69+
62
70+
55
71+
65
72+
95
73+
102
74+
117
75+
150
76+
182
77+
127
78+
219
79+
299
80+
277
81+
309
82+
576
83+
```
84+
85+
In this list, adding up all of the numbers from 15 through 40 produces the invalid number from step 1, 127. (Of course, the contiguous set of numbers in your actual list might be much longer.)
86+
87+
To find the encryption weakness, add together the smallest and largest number in this contiguous range; in this example, these are 15 and 47, producing 62.
88+
89+
What is the encryption weakness in your XMAS-encrypted list of numbers?

0 commit comments

Comments
 (0)