Repository navigation
Expand file tree
/
Copy pathday10.kk
More file actions
142 lines (123 loc) · 4.3 KB
/
Copy pathday10.kk
File metadata and controls
142 lines (123 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import util/prelude
fun parse-target-state()
char('[')
val lights = many({one-of(".#")})
char(']')
space()
var res := 0.int32
lights.foreach-indexed fn (i, char)
if char == '#' then
res := res + one.shl(i)
res
fun parse-wirings()
many
char('(')
val lights = sep-by({pint()}, {char(',')})
char(')')
space()
var res := 0.int32
lights.foreach fn (i)
res := res + one.shl(i)
res
fun parse-joltages()
char('{')
val joltages = sep-by({pint()}, {char(',')})
char('}')
joltages.map(int32)
fun parse-input()
get-input-lines().map fn (line)
line.slice.parse
(parse-target-state(), parse-wirings(), parse-joltages())
.maybe.unjust
fun press-buttons(buttons: int32, wirings: list<int32>): pure int32
var res := zero
wirings.foreach-indexed fn (d, wiring)
val shifted = one.shl(d)
if shifted.and(buttons) != zero then
res := res.xor(wiring)
res
fun solve-binary(target: int32, wirings: list<int32>): pure list<int32>
val num-buttons = wirings.length
var solutions: list<int32> := Nil
for(2 ^ num-buttons) fn (active-buttons-int)
val active-buttons = active-buttons-int.int32
val res = press-buttons(active-buttons, wirings)
if res == target then
solutions := Cons(active-buttons, solutions)
solutions
fun build-cache(wirings: list<int32>): pure hash-map<int, list<int32>>
val num-buttons = wirings.length
var cache: hash-map<int, list<int32>> := hash-map(num-buttons)
for(2 ^ num-buttons) fn (active-buttons-int)
val active-buttons = active-buttons-int.int32
val res = press-buttons(active-buttons, wirings)
match cache.get(res.int)
Just(xs) -> cache := cache.insert(res.int, Cons(active-buttons, xs))
Nothing -> cache := cache.insert(res.int, Cons(active-buttons, Nil))
cache
pub fun part-one()
parse-input().map(fn ((target, wirings, _))
solve-binary(target, wirings).map(popcount).minimum
).sum
fun build-binary(target: list<int32>): int32
val n = target.length
var res := zero
target.foreach-indexed fn (i, t)
if t.and(one) == one then
res := res.or(one.shl(i))
res
fun int32/get-max-bit(num: int32)
num.float64.log2.floor.int
fun show-binary(num: int32)
val max-bit = get-max-bit(num)
list(max-bit + 1).map(fn (i)
if num.shr(max-bit - i).and(one) == one then "1" else "0"
).join("")
/// Same idea as in https://www.reddit.com/r/adventofcode/comments/1pk87hl/2025_day_10_part_2_bifurcate_your_way_to_victory/
fun solve-decimal(target-in: list<int32>, wirings: list<int32>)
var options-cache: hash-map<int, list<int32>> := build-cache(wirings)
var result-cache: hash-map<string, int> := hash-map()
val binary-cache = build-cache(wirings)
fun solve-decimal-(target: list<int32>)
val cache-key = target.show
match result-cache.get(cache-key)
Just(res) -> return res
Nothing -> ()
if target.all(_ == zero) then
return 0
val lsbs = build-binary(target)
val options-to-even = options-cache.get(lsbs.int).default([])
val num-wirings = wirings.length
val best = options-to-even.foldl(99999, fn (best, option)
val num-active-buttons = option.popcount
val reduced-target = target.map-indexed fn (target-index, target-value)
var reduced := target-value
/// how often do i have to reduce target value i?
wirings.foreach-indexed fn (wiring-index, wiring-value)
/// if wiring influences value and is active
if one.shl(target-index).and(wiring-value) != zero &&
one.shl(wiring-index).and(option) != zero then
reduced := reduced - one
reduced
if reduced-target.any(_ < zero) then
best
else
val halved-reduced-target = reduced-target.map(fn (t) if t > zero then t.shr(1) else zero)
min(best, num-active-buttons + 2 * solve-decimal-(halved-reduced-target))
)
result-cache := result-cache.insert(cache-key, best)
best
solve-decimal-(target-in)
pub fun part-two()
val input = parse-input()
val total = input.length
input.map-indexed(fn (i, (_, wirings, joltages))
val res = solve-decimal(joltages, wirings)
// println(("("++ (i + 1).show ++ "/" ++ total.show ++ ") ").pad-left(10, ' ') ++ res.show)
res
).sum
pub fun main()
with day(10)
// with example-input
// part-one()
part-two()