Skip to content

Commit 71c175f

Browse files
committed
2022 day 10
1 parent 6685d2b commit 71c175f

File tree

3 files changed

+289
-0
lines changed

3 files changed

+289
-0
lines changed

2022/go/day10/day10.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
)
8+
9+
func main() {
10+
input, _ := os.ReadFile("../../input/day10.txt")
11+
program := parseProgram(string(input))
12+
13+
fmt.Printf("part1 : %d\n", part1(program))
14+
fmt.Printf("part2 :\n")
15+
part2(program)
16+
}
17+
18+
func part2(program []instruction) {
19+
for cycle := 1; cycle <= 6*40; cycle++ {
20+
sprite := getState(cycle, program).x
21+
pos := (cycle - 1) % 40
22+
if pos >= sprite-1 && pos <= sprite+1 {
23+
fmt.Print("#")
24+
} else {
25+
fmt.Print(" ")
26+
}
27+
if cycle%40 == 0 {
28+
fmt.Print("\n")
29+
}
30+
}
31+
}
32+
33+
func part1(program []instruction) (sum int) {
34+
for _, cycle := range []int{20, 60, 100, 140, 180, 220} {
35+
sum += cycle * getState(cycle, program).x
36+
}
37+
return sum
38+
}
39+
40+
func getState(atCycle int, program []instruction) state {
41+
state := state{x: 1}
42+
cycle := 1
43+
for _, inst := range program {
44+
cycles, new := run(inst, state)
45+
cycle = cycle + cycles
46+
if cycle > atCycle {
47+
break
48+
}
49+
state = new
50+
}
51+
return state
52+
}
53+
54+
func run(i instruction, old state) (int, state) {
55+
switch i.op {
56+
case "noop":
57+
return 1, old
58+
case "addx":
59+
return 2, state{x: old.x + i.v}
60+
}
61+
panic("error")
62+
}
63+
64+
type state struct {
65+
x int
66+
}
67+
68+
type instruction struct {
69+
op string
70+
v int
71+
}
72+
73+
func parseProgram(input string) (program []instruction) {
74+
lines := strings.Split(input, "\n")
75+
for _, line := range lines {
76+
var i instruction
77+
fmt.Sscanf(line, "%s %d", &i.op, &i.v)
78+
program = append(program, i)
79+
}
80+
return program
81+
}

2022/input/day10.txt

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
noop
2+
noop
3+
noop
4+
addx 6
5+
noop
6+
addx 30
7+
addx -26
8+
noop
9+
addx 5
10+
noop
11+
noop
12+
noop
13+
noop
14+
addx 5
15+
addx -5
16+
addx 6
17+
addx 5
18+
addx -1
19+
addx 5
20+
noop
21+
noop
22+
addx -14
23+
addx -18
24+
addx 39
25+
addx -39
26+
addx 25
27+
addx -22
28+
addx 2
29+
addx 5
30+
addx 2
31+
addx 3
32+
addx -2
33+
addx 2
34+
noop
35+
addx 3
36+
addx 2
37+
addx 2
38+
noop
39+
addx 3
40+
noop
41+
addx 3
42+
addx 2
43+
addx 5
44+
addx 4
45+
addx -18
46+
addx 17
47+
addx -38
48+
addx 5
49+
addx 2
50+
addx -5
51+
addx 27
52+
addx -19
53+
noop
54+
addx 3
55+
addx 4
56+
noop
57+
noop
58+
addx 5
59+
addx -1
60+
noop
61+
noop
62+
addx 4
63+
addx 5
64+
addx 2
65+
addx -4
66+
addx 5
67+
noop
68+
addx -11
69+
addx 16
70+
addx -36
71+
noop
72+
addx 5
73+
noop
74+
addx 28
75+
addx -23
76+
noop
77+
noop
78+
noop
79+
addx 21
80+
addx -18
81+
noop
82+
addx 3
83+
addx 2
84+
addx 2
85+
addx 5
86+
addx 1
87+
noop
88+
noop
89+
addx 4
90+
noop
91+
noop
92+
noop
93+
noop
94+
noop
95+
addx 8
96+
addx -40
97+
noop
98+
addx 7
99+
noop
100+
addx -2
101+
addx 5
102+
addx 2
103+
addx 25
104+
addx -31
105+
addx 9
106+
addx 5
107+
addx 2
108+
addx 2
109+
addx 3
110+
addx -2
111+
noop
112+
addx 3
113+
addx 2
114+
noop
115+
addx 7
116+
addx -2
117+
addx 5
118+
addx -40
119+
addx 20
120+
addx -12
121+
noop
122+
noop
123+
noop
124+
addx -5
125+
addx 7
126+
addx 7
127+
noop
128+
addx -1
129+
addx 1
130+
addx 5
131+
addx 3
132+
addx -2
133+
addx 2
134+
noop
135+
addx 3
136+
addx 2
137+
noop
138+
noop
139+
noop
140+
noop
141+
addx 7
142+
noop
143+
noop
144+
noop
145+
noop

2022/java/Day10.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import java.nio.file.Files;
2+
import java.nio.file.Path;
3+
import java.util.List;
4+
import java.util.Set;
5+
import java.util.stream.Collectors;
6+
import java.util.stream.IntStream;
7+
8+
class Day10 {
9+
10+
public static void main(String ... args) throws Exception {
11+
var program = parseProgram(Files.readString(Path.of("../input/day10.txt")));
12+
System.out.printf("part1: %d\n", part1(program));
13+
System.out.printf("part2:\n%s", part2(program));
14+
}
15+
16+
static String part2(List<Instruction> program) {
17+
return IntStream.range(1, 6*40+1)
18+
.mapToObj(cycle -> pixelAtCycle(cycle, program) + (cycle%40 == 0 ? "\n" : ""))
19+
.collect(Collectors.joining());
20+
}
21+
22+
static String pixelAtCycle(int cycle, List<Instruction> program) {
23+
var middle = getState(cycle, program).x;
24+
var sprite = Set.of(middle, middle-1, middle+1);
25+
return sprite.contains((cycle-1) % 40) ? "#" : " ";
26+
}
27+
28+
static int part1(List<Instruction> program) {
29+
return IntStream.of(20,60,100,140,180,220).map(c -> c * getState(c, program).x).sum();
30+
}
31+
32+
static State getState(int atCycle, List<Instruction> program) {
33+
var state = new State(1, 1);
34+
for (var i : program) {
35+
var newState = run(i, state);
36+
if (newState.cycle > atCycle) {
37+
break;
38+
}
39+
state = newState;
40+
}
41+
return state;
42+
}
43+
44+
static State run(Instruction i, State state) {
45+
return switch (i.op) {
46+
case "noop" -> new State(state.cycle+1, state.x);
47+
case "addx" -> new State(state.cycle+2, state.x + i.value);
48+
default -> throw new IllegalArgumentException("invalid operation");
49+
};
50+
}
51+
52+
static List<Instruction> parseProgram(String input) {
53+
return input.lines().map(l -> Instruction.parse(l.split(" "))).toList();
54+
}
55+
56+
record State(int cycle, int x) {}
57+
58+
record Instruction(String op, int value) {
59+
static Instruction parse(String ... parts) {
60+
return new Instruction(parts[0], parts[0].equals("noop") ? 0 : Integer.parseInt(parts[1]));
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)